In previous tutorial, we have learned about Python file handling.
The file can be open in reading (r), writing (w) or append (a) mode. These modes can be followed by ‘+’ sign.
Sometimes, these modes are very confusing for many of us.
One such term is understanding the difference between r+ and w+ in Python.
Opening the file in the wrong mode can lead to big trouble. You can have very precious data in your file. Losing data can be hazardous. To prevent this using proper file mode is essential.
We all know, mode ‘r’ is used to open the file for reading. And mode ‘w’ is used to open the file for writing.
But, using mode ‘r+’ and ‘w+’, is confusing.
Both ‘r+’ and ‘w+’ opens the file for both reading and writing and place the pointer at the beginning.
‘r+’ vs ‘w+’ in Python (Tabular Form)
# | basis of difference | r+ | w+ |
---|---|---|---|
1 | Opening a File (if file exists) | It opens the file and places the pointer at the beginning of the file to read. | It deletes all the content of the file and keeps the point at the beginning of the file. |
2 | Creating a File (if file does not exist) | If the file is not present while opening the file in ‘r+’ mode, it throws FileNotFound exception. | If the file is not present while opening the file in ‘w+’ mode, it creates new empty file. |
3 | Reading File | You can read the complete file text. | As opening file in ‘w+’ modes deletes the file content, you can not read the file. |
4 | Writing File Content | If you open file in ‘r+’ mode and try to write the content, it start writing from the beginning and overwrite the old content with new. | It deletes all the old contents from the text file and save new text inside the file. |
Assume, we have text file called “sample.txt” with the following text in it.
Welcome to CSEstack.org. We are learning File Handling in Python.
Let’s perform read-write operations on the text file by opening the file in ‘r+’, ‘w+’ mode.
with open('sample.txt','r+') as fd:
print(fd.read())
Output:
Welcome to CSEstack.org. We are learning File Handling in Python.
with open('sample.txt','w+') as fd:
print(fd.read())
Output:
It deletes all the previous content from the file and prints nothing.
with open('sample.txt','r+') as fd:
fd.write("New text.")
Output:
It overwrite the previous content from file with the new text. If you open file, you can see the below text in it.
New text.o CSEstack.org. We are learning File Handling in Python.
with open('sample.txt','w+') as fd:
fd.write("New text.")
Output:
The file deletes all the content from the file and writes a new text in the file. If you open the file you can see below text in it.
New text.
Delete the file “sample.txt” from your computer.
As we are not performing any operating after opening a file, we are using pass
keyword. It just does nothing.
with open('sample.txt','r+') as fd:
pass
Output:
It throws exception instead of creating new file.
Traceback (most recent call last): File "file_handling.py", line 1, in with open('sample.txt','r+') as fd: FileNotFoundError: [Errno 2] No such file or directory: 'sample.txt'
If you are opening the file in ‘r+’ mode and not sure if the file exists or not, it is good practice to use exception handling. It avoids crashing your Python application if a file does not exist.
with open('sample.txt','w+') as fd:
pass
Output:
If the file is not exist, it creates new empty file.
If you understand the difference and above examples, you can make your discussion very easily. Here are some pointers.
This is simple explanation for difference between r+ and w+ in Python. If you have any doubt, write in the comment.
Hi Aniruddha,
Good explanation on file modes r+, w+. I have a query in below code:
In-Line 11 ” 5 Line” is this added at the end and not added at after 10th character like
“4 line”. How does read and write methods effect file stream(file pointer)?
Thanks a lot, Aniruddha! Finally, I got cleared on my doubts.
You’re most welcome, Venkatraman! Hope you enjoy going through other Python tutorials as well.
Plz mention the reason here