在python内置的open函数中,具体有什么区别在 w
、a
、w+
、a+
和 r+
模式之间?
In the python built-in open function, what is the exact difference between the modes w
, a
, w+
, a+
, and r+
?
特别是,文档暗示所有这些都将允许写入文件,并说它打开文件是为了附加"、写入"和更新",但没有定义这些术语的含义.
In particular, the documentation implies that all of these will allow writing to the file, and says that it opens the files for "appending", "writing", and "updating" specifically, but does not define what these terms mean.
打开方式与C标准库函数fopen()
完全相同.
The opening modes are exactly the same as those for the C standard library function fopen()
.
BSD fopen
手册页 将它们定义如下:
The BSD fopen
manpage defines them as follows:
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.
这篇关于内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!