医生说,
模式'r+', 'w+'和'a+'打开文件进行更新(注意'w+'会截断文件)。在区分二进制文件和文本文件的系统上,在模式后追加'b'以二进制模式打开文件;在没有这种区别的系统中,添加“b”没有任何效果。
这里
w+:打开文件进行读写操作。如果文件存在,则覆盖现有文件。如果该文件不存在,则创建一个新文件进行读写。
但是,如何用w+读取打开的文件?
医生说,
模式'r+', 'w+'和'a+'打开文件进行更新(注意'w+'会截断文件)。在区分二进制文件和文本文件的系统上,在模式后追加'b'以二进制模式打开文件;在没有这种区别的系统中,添加“b”没有任何效果。
这里
w+:打开文件进行读写操作。如果文件存在,则覆盖现有文件。如果该文件不存在,则创建一个新文件进行读写。
但是,如何用w+读取打开的文件?
当前回答
如h4z3所述, 在实际应用中, 有时你的数据太大,不能直接加载所有内容,或者你有一个生成器,或者实时传入的数据,你可以使用w+存储在一个文件中,然后读取。
其他回答
下面的清单可能会有帮助
角色的意义
'r' -打开读取(默认)
'w' -打开写入,首先截断文件
'x' -打开独占创建,如果文件已经存在则失败
'a' -打开用于写入,如果存在则附加到文件的末尾
'b' -二进制模式
't' -文本模式(默认)
'+' -打开更新(读取和写入)
默认模式是'r'(用于阅读文本,'rt'的同义词)。模式'w+'和'w+b'打开并截断文件。模式'r+'和'r+b'打开文件,没有截断。
参考:https://docs.python.org/3/library/functions.html开放
Python中的所有文件模式
r for reading r+ opens for reading and writing (cannot truncate a file) w for writing w+ for writing and reading (can truncate a file) rb for reading a binary file. The file pointer is placed at the beginning of the file. rb+ reading or writing a binary file wb+ writing a binary file a+ opens for appending ab+ Opens a file for both appending and reading in binary. The file pointer is at the end of the file if the file exists. The file opens in the append mode. x open for exclusive creation, failing if the file already exists (Python 3)
我也很困惑……但也许我的答案会帮助到别人。 我假设您希望利用“w+”模式来创建不存在的文件。
实际上,如果文件不存在,只能创建w, w+, a, a+。
但是如果你需要读取文件的数据(当有数据的文件确实存在的情况下),你不能用w+开箱即用,因为它会截断文件。哎呀,你不是那个意思!
所以,你最好的朋友可能是file.seek(0)的+:
with open('somefile.txt', 'a+') as f:
f.seek(0)
for line in f:
print(f.readline())
如h4z3所述, 在实际应用中, 有时你的数据太大,不能直接加载所有内容,或者你有一个生成器,或者实时传入的数据,你可以使用w+存储在一个文件中,然后读取。
下面是打开文件的不同模式:
r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. rb 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. wb 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. a 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. 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.