被python文件模式“w+搞糊涂了”

2024-04-24 03:55:34 发布

您现在位置:Python中文网/ 问答频道 /正文

doc

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the 'b' has no effect.

here

w+ : Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

但是,如何读取用w+打开的文件?


Tags: andthetofordocthatonmode
3条回答

以下是打开文件的不同模式的列表:

  • Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.

  • Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.

  • 右+

    Opens a file for both reading and writing. The file pointer will be at the beginning of the file.

  • 铷+

    Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.

  • 西

    Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

  • 世界银行

    Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

  • 西+

    Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

  • 世界银行+

    Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

  • 一个

    Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

  • ab公司

    Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

  • 一个+

    Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

  • ab公司+

    Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

假设您正在使用with语句打开文件,就像您应该这样。然后,您可以执行以下操作来读取文件:

with open('somefile.txt', 'w+') as f:
    # Note that f has now been truncated to 0 bytes, so you'll only
    # be able to read data that you write after this point
    f.write('somedata\n')
    f.seek(0)  # Important: return to the top of the file before reading, otherwise you'll just read an empty string
    data = f.read() # Returns 'somedata\n'

注意f.seek(0)——如果您忘记了这一点,f.read()调用将尝试从文件末尾读取,并返回一个空字符串。

Python中的所有文件模式

  • r用于读取
  • r+打开进行读写(无法截断文件)
  • w用于写作
  • w+用于写入和读取(可以截断文件)
  • rb用于读取二进制文件。文件指针放在文件的开头。
  • rb+读取或写入二进制文件
  • wb+写入二进制文件
  • a+打开以附加
  • ab+打开一个文件,以二进制形式进行追加和读取。如果文件存在,则文件指针位于文件末尾。文件以追加模式打开。
  • x以独占方式创建打开,如果文件已存在则失败(Python 3)

相关问题 更多 >