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.
但是,如何读取用
以下是打开文件的不同模式列表:
- 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)
我们假设您像应该的那样用一个
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 ' |
注意
文件被截断,因此您可以调用
我怀疑有两种方法来处理我认为你想要达到的目标。
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) |
希望这有帮助。
实际上,关于
1 2 3 | hello1 ok2 byebye3 |
而PY脚本:
1 2 3 | with open("test.in", 'r+')as f: f.readline() f.write("addition") |
执行后,
1 2 3 4 | hello1 ok2 byebye3 addition |
但是,当我们将脚本修改为:
1 2 | with open("test.in", 'r+')as f: f.write("addition") |
1 2 | additionk2 byebye3 |
因此,如果不进行读取操作,
顺便说一下,如果我们在