This article shows the difference between r, r+, w, w+, a and a+ in Python open() function.
Table of contents
- 1. Difference between r, r+, w, w+, a and a+
- 2. What is + means in open()?
- 3. Difference between r and r+ in open()
- 4. Difference between w and w+ in open()
- 5. Difference between a and a+ in open()
- 6. Difference between r+ and w+ in open()
- 7. Difference between r+ and a+ in open()
- 8. Difference between w+ and a+ in open()
- 9. Download Source Code
- 10. References
P.S Tested with Python 3.8
1. Difference between r, r+, w, w+, a and a+
1.1 Read below table for the difference between open modes r, r+, w, w+, a and a+ in open() function.
| r | r+ | w | w+ | a | a+ | |
|---|---|---|---|---|---|---|
| read | * | * | * | * | ||
| write | * | * | * | * | * | |
| create | * | * | * | * | ||
| truncate | * | * | ||||
| position at start | * | * | * | * | ||
| position at end | * | * |
P.S In this context, truncate means delete the content of the file.
1.2 Definition of open modes r, r+, w, w, a, a+:
- The
rthrows an error if the file does not exist or opens an existing filewithout truncatingit forreading; the file pointer position at the beginning of the file. - The
r+throws an error if the file does not exist or opens an existing filewithout truncatingit forreading and writing; the file pointer position at the beginning of the file. - The
wcreates a new file ortruncates an existing file, then opens it forwriting; the file pointer position at the beginning of the file. - The
w+creates a new file ortruncates an existing file, then opens it forreading and writing; the file pointer position at the beginning of the file. - The
acreates a new file or opens an existing file forwriting; the file pointer position at theend of the file. - The
a+creates a new file or opens an existing file forreading and writing, and the file pointer position at theend of the file.
2. What is + means in open()?
The + adds either reading or writing to an existing open mode, aka update mode.
- The
rmeansreadingfile;r+meansreading and writingthe file. - The
wmeanswritingfile;w+meansreading and writingthe file. - The
ameanswritingfile, append mode;a+meansreading and writingfile, append mode.
3. Difference between r and r+ in open()
The r+ adds writing file to the existing r mode.
A text file for testing.
welcome to python 1
welcome to python 2
welcome to python 3
welcome to python 4
3.1 Read a file with r
with open('file.txt') as f: # default `r` mode
print(f.read())
Output
welcome to python 1
welcome to python 2
welcome to python 3
welcome to python 4
In r mode, if we write the file, Python throws io.UnsupportedOperation: not writable
with open('file.txt', 'r') as f: # default `r` mode
f.write("test \n") # throws UnsupportedOperation
3.2 Read and write a file with r+
In r+ mode, we can read and write the file, but the file pointer position is at the beginning of the file; if we write the file directly, it will overwrite the beginning content.
See the below example:
with open('file.txt', 'r+') as f:
f.write("new line \n")
Output
new line
python 1
welcome to python 2
welcome to python 3
welcome to python 4
The below example uses f.read() to move the file pointer to the end of the file, and append a new line.
# alternative, open with `a` mode
with open('file.txt', 'r+') as f:
f.read() # move file position to the end of the file.
f.write("new line \n")
Output
welcome to python 1
welcome to python 2
welcome to python 3
welcome to python 4
new line
Please comment below if you know other r+ examples, thanks.
4. Difference between w and w+ in open()
The w+ adds reading file to the existing w mode.
4.1 Write a file with w
with open('file.txt', 'w+') as f: # create a new file or truncates it
f.write("test 1\n")
f.write("test 2\n")
f.write("test 3\n")
Output
test 1
test 2
test 3
4.2 Read and write a file with w+
with open('file.txt', 'w+') as f: # create a new file or truncates it
f.write("test 1\n")
f.write("test 2\n")
f.write("test 3\n") # now the file pointer is at the end
f.seek(0) # move the file pointer to the beginning
lines = f.read() # read it, now we can read!
print(lines) # print it
Output
test 1
test 2
test 3
5. Difference between a and a+ in open()
The a+ adds reading file to the existing a mode.
A text file for testing.
welcome to python 1
welcome to python 2
welcome to python 3
welcome to python 4
5.1 Append a file with a
with open('file.txt', 'a') as f:
f.write("4")
Output
welcome to python 1
welcome to python 2
welcome to python 3
welcome to python 4
4
5.2 Append a file with a+
- Open a file for reading and writing with
a+. - Counts the number of the lines.
- Append the result to the file.
with open('file.txt', 'a+') as f:
f.seek(0) # file pointer at end, move to beginning
lines = f.readlines() # read all and file pointer at end again
f.write("\n" + str(len(lines))) # append number of lines to a file
Output
welcome to python 1
welcome to python 2
welcome to python 3
welcome to python 4
4
6. Difference between r+ and w+ in open()
Below is the difference between r+ and w+:
- If the file does not exist,
r+throwsFileNotFoundError; thew+creates the file. - If the file exists,
r+opens it without truncating; thew+truncates the file and opens it.
7. Difference between r+ and a+ in open()
Below is the difference between r+ and a+:
- If the file does not exist,
r+throwsFileNotFoundError; thea+creates the file. - For
r+mode, the initial file pointer position at thebeginning of the file; Fora+mode, the initial file pointer position at theend of the file.
8. Difference between w+ and a+ in open()
Below is the difference between w+ and a+:
- If the file exists,
w+truncates the file and opens it;a+opens it without truncating. - For
w+mode, the initial file pointer position at thebeginning of the file; Fora+mode, the initial file pointer position at theend of the file.
9. Download Source Code
$ git clone https://github.com/mkyong/python
$ cd io
4.1 Write a file with w
with open(‘file.txt’, ‘w+’) as f: # create a new file or truncates it
you’ve made a small mistake there. it won’t be w+
very helpful!
Thanks so much
It was much useful. Thank u.