困惑的python文件模式“w +”

Confused by python file mode “w+”

来自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.

这里

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+打开的文件?


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

  • R

    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.

  • R+

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

  • Rb+

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

  • W

    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.

  • 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.

  • WB+

    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.

  • 抗体

    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.

  • A+

    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.


python中的所有文件模式

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


我们假设您像应该的那样用一个with语句打开文件。然后您可以这样做来读取文件:

1
2
3
4
5
6
7
8
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
'
)
    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
'

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


文件被截断,因此您可以调用read()(与使用"w"打开时不同,不会引发异常),但会得到一个空字符串。


我怀疑有两种方法来处理我认为你想要达到的目标。

1)很明显,是打开文件只读,将其读到内存中,然后用t打开文件,然后写下更改。

2)使用低级文件处理例程:

1
2
# Open file in RW , create if it doesn't exist. *Don't* pass O_TRUNC
 fd = os.open(filename, os.O_RDWR | os.O_CREAT)

希望这有帮助。


实际上,关于r+模式的所有其他答案都有问题。

test.in文件内容:

1
2
3
hello1
ok2
byebye3

而PY脚本:

1
2
3
with open("test.in", 'r+')as f:
    f.readline()
    f.write("addition")

执行后,test.in的内容将更改为:

1
2
3
4
hello1
ok2
byebye3
addition

但是,当我们将脚本修改为:

1
2
with open("test.in", 'r+')as f:
    f.write("addition")

test.in也做了响应:

1
2
additionk2
byebye3

因此,如果不进行读取操作,r+模式将允许我们从一开始就覆盖内容。如果我们做一些读取操作,f.write()将只追加到文件中。

顺便说一下,如果我们在f.write(write_content)之前输入cx1〔20〕,那么写入内容将从位置(0,0)覆盖它们。