916

In Python's built-in open function, what is the difference between the modes w, a, w+, a+, and r+?

The documentation implies that these all allow writing to the file, and says that they open the files for "appending", "writing", and "updating" specifically, but does not define these terms.

2
  • 15
    The link you provided exactly defines the values. What part about the link you provided could you not see or understand? Could you clarify your question to explain what you didn't understand about the link? Commented Sep 23, 2009 at 14:00
  • 12
    is there no simple and single doc that explains what the + sign means? Commented Jun 28, 2016 at 0:36

9 Answers 9

1048

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. 
Sign up to request clarification or add additional context in comments.

14 Comments

NOTE:Python v3 adds a number of additional modes. link to docs
Noted that w and w+ both can do The file is created if it does not exist
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.
am I right to say that the + doesn't do something consistent independent if it is a,w or r? Or am I failing to see the pattern? What is the pattern?
@CharlieParker: I believe the pattern is "also do the other thing". So, r+ doesn't just open the file for reading but also for writing and w+, in turn, doesn't just open it for writing but also for reading. Similarly for a+.
|
891

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.

10 Comments

The a description is wrong. The writes are always positioned at the end.
@And I believe @Antti is referring to the property Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar which is somewhat stronger than just saying the initial position is the end.
@CharlieParker That there are basically two file operations (read, write). Mode r is primarily for reading, modes w, a are primarily for writing. And the plus sign enables the second operation for a given mode (simply said).
For posterity: truncate means to overwrite from the beginning.
@MinhTran You are not overwriting existing contents, you are first throwing it away and then write as if there is nothing there. If the file contains the string aaaaa and I truncate and write ttt to the file, the result wouldn't be tttaa, but just ttt.
|
326

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

11 Comments

Why is there no "Create file if it doesn't exist. If it does exist, position at start, enable read and write"? This is the most obvious use-case for me: I'm storing data in a file. If the file's not there, create it instead of erroring. If there's data in the file I want to read it all from the top, update some stuff then completely re-write the file from 0 for the NEXT TIME I load it. I use open(file,'a'); close(); open(file,'r+') to accomplish this.
@pinhead What you are describing is more appropriately handled by opening the file in read mode, loading the contents into memory, and closing it, then opening it afterwards in write mode to write out when you're done. I assume from the use case that you describe that you want the whole file in memory, and this way you don't corrupt the file in case your program terminates before it has time to save and exit.
What does "truncating" mean in this context?
@CharlieParker It means that all content of the file is erased (file is made empty)
What about updating the table, to include 'x' for Python 3?
|
62
r r+ x x+ w w+ a a+
readable
writable
default position: start
default position: end
must exist
mustn't exist
truncate (clear file) on load
always write to EOF

Mode

t (default) b
str (io.TextIOBase)
bytes (io.BufferedIOBase)

If no mode is selected, text mode (t) is used. As such r is the same as rt.

1 Comment

@qff EOF stands for End Of File. The "always" means even if you seek(0) (move the cursor to the start of the file) then when you write("foo") the file will write to the end of the file. Hence why it's called "append" mode. You always append to the end of the file.
48

The options are the same as for the fopen function in the C standard library:

w truncates the file, overwriting whatever was already there

a appends to the file, adding onto whatever was already there

w+ opens for reading and writing, truncating the file but also allowing you to read back what's been written to the file

a+ opens for appending and reading, allowing you both to append to the file and also read its contents

Comments

15

I find it important to note that Python 3 defines the opening modes differently to the answers here that were correct for Python 2.

The Python 3 opening modes are:

'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) 

The modes r, w, x, a are combined with the mode modifiers b or t. + is optionally added, U should be avoided.

As I found out the hard way, it is a good idea to always specify t when opening a file in text mode since r is an alias for rt in the standard open() function but an alias for rb in the open() functions of all compression modules (when e.g. reading a *.bz2 file).

Thus the modes for opening a file should be:

rt / wt / xt / at for reading / writing / creating / appending to a file in text mode and

rb / wb / xb / ab for reading / writing / creating / appending to a file in binary mode.

Use + as before.

Comments

14

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.

Comments

11

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.

This is directly quoted from Python Software Foundation 2.7.x.

Comments

3

The particular doubt that many people get is 'What is the difference between r+ and w+ modes?

The r+ helps you read and write data onto an already existing file without truncating (Error if there is no such file).

The w+ mode on the other hand also allows reading and writing but it truncates the file (if no such file exists - a new file is created). If you are wondering how it is possible to read from a truncated file, the reading methods can be used to read the newly written file (or the empty file).

Cheers!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.