As in, when opening a file with open()
, why do we have modes 'r+', 'w+', 'a+'? Surely mode 'a' does the same job? I'm especially confused by the difference between modes 'a' and 'a+' - could someone explain where they differ and, if possible, when one should use one or the other?
Asked
Active
Viewed 2,089 times
-1

tshepang
- 12,111
- 21
- 91
- 136

Enchanted_Eggs
- 53
- 1
- 8
2 Answers
3
The opening modes are exactly the same that C fopen() std library function.
The BSD fopen manpage defines them as follows:
``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 only difference between a and a+ is that a+ allows reading of files.
See this post for more info.

Community
- 1
- 1
-
Out of curiosity, did you [copy and paste this answer from somewhere](http://meta.stackexchange.com/questions/78658/is-it-okay-to-copy-paste-answers-from-other-questions)? β Joseph Dunn Aug 15 '13 at 15:04
-
@JosephDunn Yes, I will edit in a link. β Aug 15 '13 at 15:05
-
@JosephDunn Sorry Joseph, won't happen again. β Aug 15 '13 at 15:10
0
Quoted from the Python 2.7 tutorial:
mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end. 'r+' opens the file for both reading and writing. The mode argument is optional; 'r' will be assumed if itβs omitted.
'a' opens the file in write (append at end of file) mode, while 'r+' opens it in both read and write (insert at start of file).