Difference between modes a, a+, w, w+, and r+ in built-in open function?
Asked Answered
L

9

864

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.

Larkins answered 23/9, 2009 at 13:27 Comment(2)
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?Longsome
is there no simple and single doc that explains what the + sign means?Firenew
T
987

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.
Threnody answered 23/9, 2009 at 13:33 Comment(14)
I believe you mean the fopen call in the C standard library (which is not a system call)Precincts
Indeed there are significant differences between the two. Ex: 'a+' in sys call positions the stream at beginning of file, in std lib it positions at end. Fixing link...Aplasia
NOTE:Python v3 adds a number of additional modes. link to docsComponent
Noted that w and w+ both can do The file is created if it does not existRhapsody
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.Kief
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?Firenew
Why are the default Python docs so crappy? docs.python.org/2/library/functions.html#open Is there a nice clean set of docs for the built-in functions like the BSD manpage you linked to?Nightspot
@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+.Bonine
a does not create a file if it doesn't exit. a+ surely does.Centesimal
Why does w+ exist? I'm finding that the file is being truncated before I can read it. If I use "r" I can use readline() and get my first line. If I use "w+" readline() returns empty string.Sussi
@Centesimal are you saying that the docs in the answer are wrong? They explicitly say a creates the file if it doesn't exist. a+ seems to just add the ability to read as well as append, but it's not very clear.Dimitri
@johnstamos > Why does w+ exist?. Because you might want to do a read with the same file pointer before closing it.Tucana
These descriptions are somewhat incomplete in my opinion. e.g. w+ says it truncates but then it doesn't clarify if what that means but in w it says it truncates to length zero. Am I suppose to assume the truncation is the same for both?Firenew
@Threnody then of course this first comment in the docs saying it trucates to zero is redundant and thus prone to confusing the reader. It leads to the very natural question as to why the "to zero" was clarified in one and not the other...regardless, thanks for the comment.Firenew
A
827

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.

Aesculapius answered 1/6, 2015 at 5:13 Comment(10)
So the + basically means writing. That is weird that the w doesn't mean that but it means truncating...(after reading the next answer, it seems w writes over and a stands for appending. That makes more sense...) Do you have any comments on file creation if it doesn't exist?Firenew
File creation happens for all methods except r and r+ (where you are assuming there IS a file to initially read...).Spotted
The a description is wrong. The writes are always positioned at the end.Roxannaroxanne
@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.Hockey
The plus does not mean "write". With "w" It means create or overwrite, where overwrite means make the file zero bytes to start with even if it had previously had more bytes of content.Kelikeligot
for those who are wondering about the difference between r+b and w+b, from the version 3 documentation: For binary read-write access, the mode 'w+b' opens and truncates the file to 0 bytes. 'r+b' opens the file without truncation.Fatback
@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).Doggerel
For posterity: truncate means to overwrite from the beginning.Ed
@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.Flemings
w+ doesn't allow reading the file before writing to it.Foresaid
B
305

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

Blackjack answered 19/6, 2015 at 6:26 Comment(11)
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.Radiative
@Radiative 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.Honeysuckle
What does "truncating" mean in this context?Firenew
@CharlieParker It means that all content of the file is erased (file is made empty)Blackjack
You might want to add a note that with a and a+ writes will always happen at the end of the file, irrespective of whether one manually moves the pointer using seek().Bonine
@Bonine thanks. I didn't know that. Thanks. After some testing - I'm stunned. Seems a modes do not play well with seek movements.Blackjack
What about updating the table, to include 'x' for Python 3?Sedimentation
@Blackjack as @NikosAlexandris says, the table could include the x option introduced in Python 3.3+. I have a (rejected) edit which I hope suffices.Ratel
@Radiative the reason is this: why position at the start if you are writing and file already exists? Wouldn't that start overwriting contents? a+ will keep the file pointer at the end for this reason. I can think of one reason to open in read+write mode and create if not exists and also have random access for one reason and that is a fixed-size shared memory mapped file.Armistice
This is still accurate in Python 3.8.1. I expanded your table over at dev.to/codemouse92/dead-simple-python-working-with-files-lmg (and in my upcoming book); you're welcome to incorporate my changes in if you like.Interlocution
I had the same question as @pinhead, and I don't think @Honeysuckle answered it. You can use a+ to create a file that might not exist and use .seek(0) to read (not write) from the beginning. That way you don't have to use open(file,'a'); close(); open(file,'r+').Handrail
P
44

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

Precincts answered 23/9, 2009 at 13:33 Comment(2)
What does "truncating" mean in this context? Does it mean to delete the old data if it had some? Or something else more specific?Firenew
@CharlieParker: Correct - it means that all data in the existing file will be dropped and we begin writing from the beginning of a now-empty file.Precincts
M
43
r r+ x x+ w w+ a a+
readable
writeable
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.

Minh answered 16/5, 2021 at 15:7 Comment(4)
What does "Always write to EOF" mean?Octagon
@Octagon 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.Minh
@Vopel Yes, I have rejected your edit twice. The edit changed the Markdown with no appreciable benefit to the output, however you also mangled the second table tipping the edit closer to (unintentional) vandalism. "and then making a minor change was not cool" never happened. You should be able to see the last edit is from May 6, 2023 by Good Pen. I see no reason to edit my answer, unless another mode for open is added.Minh
@Vopel In my previous comment I mentioned two problems: 1. no appreciable benefit to the post, and 2. mangling the second table. Fixing the second issue still leaves the first issue. Your second and third edit don't have any appreciable benefit. If you can find something to improve the answer then feel free to edit, but trying to get the same changes to MD passed will just be met with me rejecting your edit.Minh
B
11

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.

Bogor answered 24/9, 2014 at 1:9 Comment(0)
A
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.

Anemone answered 5/1, 2017 at 1:51 Comment(0)
M
10

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.

Mecham answered 24/2, 2021 at 6:3 Comment(0)
L
0

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!

Libertinage answered 12/12, 2022 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.