python – Difference between modes a, a+, w, w+, and r+ in built-in open function?

python – Difference between modes a, a+, w, w+, and r+ in built-in open function?

The opening modes are exactly the same as those for the C standard library function 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.

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.

src=https://d20p74l5mne5au.cloudfront.net/ExWNT-white-bg.png/

python – Difference between modes a, a+, w, w+, and r+ in built-in open function?

Same info, just in table form

                  | 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

  • truncate – 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

Leave a Reply

Your email address will not be published.