From the doc,
Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the 'b' has no effect.
and here
w+ : Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
But, how to read a file open with w+
?
Let's say you're opening the file with a with
statement like you should be. Then you'd do something like this to read from your file:
with open('somefile.txt', 'w+') as f:
# Note that f has now been truncated to 0 bytes, so you'll only
# be able to read data that you write after this point
f.write('somedata\n')
f.seek(0) # Important: return to the top of the file before reading, otherwise you'll just read an empty string
data = f.read() # Returns 'somedata\n'
Note the f.seek(0)
-- if you forget this, the f.read()
call will try to read from the end of the file, and will return an empty string.
The file is truncated, so you can call read()
(no exceptions raised, unlike when opened using 'w') but you'll get an empty string.
I suspect there are two ways to handle what I think you'r trying to achieve.
1) which is obvious, is open the file for reading only, read it into memory then open the file with t, then write your changes.
2) use the low level file handling routines:
# Open file in RW , create if it doesn't exist. *Don't* pass O_TRUNC
fd = os.open(filename, os.O_RDWR | os.O_CREAT)
Hope this helps..
r
for read
w
for write
r+
for read/write without deleting the original content if file exists, otherwise raise exception
w+
for delete the original content then read/write if file exists, otherwise create the file
For example,
>>> with open("file1.txt", "w") as f:
... f.write("ab\n")
...
>>> with open("file1.txt", "w+") as f:
... f.write("c")
...
$ cat file1.txt
c$
>>> with open("file2.txt", "r+") as f:
... f.write("ab\n")
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'file2.txt'
>>> with open("file2.txt", "w") as f:
... f.write("ab\n")
...
>>> with open("file2.txt", "r+") as f:
... f.write("c")
...
$ cat file2.txt
cb
$
Actually, there's something wrong about all the other answers about r+
mode.
test.in
file's content:
hello1
ok2
byebye3
And the py script's :
with open("test.in", 'r+')as f:
f.readline()
f.write("addition")
Execute it and the test.in
's content will be changed to :
hello1
ok2
byebye3
addition
However, when we modify the script to :
with open("test.in", 'r+')as f:
f.write("addition")
the test.in
also do the respond:
additionk2
byebye3
So, the r+
mode will allow us to cover the content from the beginning if we did't do the read operation. And if we do some read operation, f.write()
will just append to the file.
By the way, if we f.seek(0,0)
before f.write(write_content)
, the write_content will cover them from the positon(0,0).
Both seems to be working same but there is a catch.
r+ :-
w+ :-
So, Overall saying both are meant to open the file to read and write but difference is whether we want to erase the data in the beginning and then do read/write or just start as it is.
abc.txt
- in beginning
1234567
abcdefg
0987654
1234
Code for r+
with open('abc.txt', 'r+') as f: # abc.txt should exist before opening
print(f.tell()) # Should give ==> 0
f.write('abcd')
print(f.read()) # Pointer is pointing to index 3 => 4th position
f.write('Sunny') # After read pointer is at End of file
Output
0
567
abcdefg
0987654
1234
abc.txt
- After Run:
abcd567
abcdefg
0987654
1234Sunny
Resetting abc.txt as initial
Code for w+
with open('abc.txt', 'w+') as f:
print(f.tell()) # Should give ==> 0
f.write('abcd')
print(f.read()) # Pointer is pointing to index 3 => 4th position
f.write('Sunny') # After read pointer is at End of file
Output
0
abc.txt
- After Run:
abcdSunny
As mentioned by h4z3, For a practical use, Sometimes your data is too big to directly load everything, or you have a generator, or real-time incoming data, you could use w+ to store in a file and read later.
All file modes in Python
r
for readingr+
opens for reading and writing (cannot truncate a file)w
for writing w+
for writing and reading (can truncate a file) rb
for reading a binary file. The file pointer is placed at the beginning of the file.rb+
reading or writing a binary filewb+
writing a binary filea+
opens for appendingab+
Opens a file for both appending and reading in binary. The file pointer is at the end of the file if the file exists. The file opens in the append mode.x
open for exclusive creation, failing if the file already exists (Python 3)Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.
Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
Source: Stackoverflow.com