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.
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.
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.
I noticed that every now and then I need to Google fopen all over again, just to build a mental image of what the primary differences between the modes are. So, I thought a diagram will be faster to read next time. Maybe someone else will find that helpful too.
回答 2
相同信息,只是表格形式
| r r+ w w+ a a+------------------|--------------------------
read |++++
write |+++++
write after seek |+++
create |++++
truncate |++
position at start |++++
position at end |++
| r r+ w w+ a a+
------------------|--------------------------
read | + + + +
write | + + + + +
write after seek | + + +
create | + + + +
truncate | + +
position at start | + + + +
position at end | + +
where meanings are:
(just to avoid any misinterpretation)
read – reading from file is allowed
write – writing to file is allowed
create – file is created if it does not exist yet
trunctate – during opening of the file it is made empty (all content of the file is erased)
position at start – after file is opened, initial position is set to the start of the file
position at end – after file is opened, initial position is set to the end of the file
Note: a and a+ always append to the end of file – ignores any seek movements.
BTW. interesting behavior at least on my win7 / python2.7, for new file opened in a+ mode: write('aa'); seek(0, 0); read(1); write('b') – second write is ignored write('aa'); seek(0, 0); read(2); write('b') – second write raises IOError
I think this is important to consider for cross-platform execution, i.e. as a CYA. :)
On Windows, ‘b’ appended to the mode opens the file in binary mode, so there are also modes like ‘rb’, ‘wb’, and ‘r+b’. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a ‘b’ to the mode, so you can use it platform-independently for all binary files.
我碰巧试图弄清楚为什么要使用模式“ w +”与“ w”。最后,我只是做了一些测试。我看不到’w +’模式有什么用,因为在两种情况下,文件都是从头开始被截断的。但是,有了“ w +”,您可以在写完后通过回头阅读。如果您尝试使用“ w”进行任何读取,则将引发IOError。在模式’w +’下不使用seek进行读取不会产生任何结果,因为文件指针将位于您写入的位置之后。
I hit upon this trying to figure out why you would use mode ‘w+’ versus ‘w’. In the end, I just did some testing. I don’t see much purpose for mode ‘w+’, as in both cases, the file is truncated to begin with. However, with the ‘w+’, you could read after writing by seeking back. If you tried any reading with ‘w’, it would raise an IOError. Reading without using seek with mode ‘w+’ isn’t going to yield anything, since the file pointer will be after where you have written.