在python内置的开放函数中,w、a、w+、a+和r+模式之间的确切区别是什么?
特别是,文档暗示所有这些都将允许写入文件,并表示它将打开文件以进行“附加”、“写入”和“更新”,但没有定义这些术语的含义。
在python内置的开放函数中,w、a、w+、a+和r+模式之间的确切区别是什么?
特别是,文档暗示所有这些都将允许写入文件,并表示它将打开文件以进行“附加”、“写入”和“更新”,但没有定义这些术语的含义。
当前回答
r | r+ | x | x+ | w | w+ | a | a+ | |
---|---|---|---|---|---|---|---|---|
readable | x | x | x | x | x | |||
writeable | x | x | x | x | x | x | x | |
default position: start | x | x | x | x | x | x | ||
default position: end | x | x | ||||||
must exist | x | x | ||||||
mustn't exist | x | x | ||||||
truncate (clear file) on load | x | x | ||||||
Always write to EOF | x | x |
Mode
t (default) | b | |
---|---|---|
str (io.TextIOBase ) |
x | |
bytes (io.BufferedIOBase ) |
x |
如果未选择任何模式;使用文本模式(t)。因此,r与rt相同。
其他回答
我发现需要注意的是,python3定义的打开模式与这里的答案不同,这些答案对Python2来说是正确的。
Python 3打开模式包括:
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
----
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newlines mode (for backwards compatibility; should not be used in new code)
模式r、w、x、a与模式修改器b或t组合在一起。如果选择添加+,则应避免使用U。
正如我发现的那样,在文本模式下打开文件时始终指定t是一个好主意,因为r是标准open()函数中rt的别名,但在所有压缩模块的open()功能中rb的别名(例如,读取*.bz2文件时)。
因此,打开文件的模式应为:
rt/wt/xt/at用于以文本模式读取/写入/创建/附加到文件,以及
rb/wb/xb/ab,用于以二进制模式读取/写入/创建/追加文件。
如前所述使用+。
我认为这对于跨平台执行(即作为CYA)来说非常重要。:)
在Windows上,附加到模式的“b”以二进制模式打开文件,因此也有类似“rb”、“wb”和“r+b”的模式。Windows上的Python区分了文本文件和二进制文件;当读取或写入数据时,文本文件中的行尾字符会自动轻微更改。这种对文件数据的幕后修改对于ASCII文本文件来说很好,但它会破坏二进制数据,就像JPEG或EXE文件中的二进制数据一样。在读取和写入此类文件时,请非常小心地使用二进制模式。在Unix上,在模式中附加一个“b”并不有害,因此您可以对所有二进制文件独立地使用它。
这直接引用自Python Software Foundation 2.7.x。
打开模式与C标准库函数fopen()的打开模式完全相同。
BSD fopen手册页对它们的定义如下:
The argument mode points to a string beginning with one of the following
sequences (Additional characters may follow these sequences.):
``r'' Open text file for reading. The stream is positioned at the
beginning of the file.
``r+'' Open for reading and writing. The stream is positioned at the
beginning of the file.
``w'' Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.
``w+'' Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at
the beginning of the file.
``a'' Open for writing. The file is created if it does not exist. The
stream is positioned at the end of the file. Subsequent writes
to the file will always end up at the then current end of file,
irrespective of any intervening fseek(3) or similar.
``a+'' Open for reading and writing. The file is created if it does not
exist. The stream is positioned at the end of the file. Subse-
quent writes to the file will always end up at the then current
end of file, irrespective of any intervening fseek(3) or similar.
许多人特别疑惑的是“r+和w+模式之间有什么区别?”?
r+帮助您在不截断的情况下将数据读写到已经存在的文件上(如果没有这样的文件,则为错误)。
另一方面,w+模式也允许读取和写入,但它会截断文件(如果不存在这样的文件,则会创建一个新文件)。如果您想知道如何从截断的文件中读取,可以使用读取方法来读取新写入的文件(或空文件)。
干杯
选项与C标准库中fopen函数的选项相同:
w截断文件,覆盖已经存在的内容
a附加到文件,添加到已经存在的文件中
w+打开用于读取和写入,截断文件,但也允许您读取已写入文件的内容
a+打开用于附加和读取,允许您附加到文件并读取其内容