About w and w+
Write only or w mode opens the file in writing only mode. This mode creates a new file if the file does not exist. Otherwise, it just overwrites the existing file.
Write and read or w+ mode opens the file for writing as well as reading. It overwrites the file if it exists; else, it creates a new file for writing and reading.
The plus(+) sign either adds reading or writing mode to an existing write or read mode.
Also see, Python Filter Function
Difference between r+ and w+
Let's now discuss the key differences between r+ and w+ modes in Python:
-
Opening a file: r+ mode opens the file if it exists, while w+ mode also opens the file, but it deletes all the content present in the file. The pointer in both cases is present at the start of the file.
-
Making a new file: If the file does not exist, r+ throws an exception error of 'filenotfound' while w+ creates a new empty file. No error message is thrown in w+ mode.
-
Reading a file: r+ helps in the complete reading of the file, while w+ doesn't allow the same. As opening a file in w+ erases all the contents of the file, one can not read the content present inside the file.
-
Writing a file: r+ overwrites the file with the new content from the beginning of the document, while w+ deletes all the old content and then adds the new text to the file.
- Error Message: r+ throws an exception or an error message if the file does not exist, while w+ does not throw any error message; instead, it creates a new file.
Operations on r+ and w+
Consider a file named 'ninja.txt' with the content 'Coding Ninjas - Become a Ninja'.
Read operation
Below are the examples of read operation using r+ and w+ mode.
Using r+ mode:
with open('ninja.txt','r+') as cn:
print(cn.read())

You can also try this code with Online Python Compiler
Run Code
The above code reads the file from the beginning, and the output of this code will be
Coding Ninjas - Become a Ninja
Using w+ mode:
with open('ninja.txt','w+') as cn:
print(cn.read())

You can also try this code with Online Python Compiler
Run Code
As in w+ mode, the old content of the file gets erased, so we would not see anything in the output.
Write operation
Below are the examples of write operation using r+ and w+ mode.
Using r+ mode:
with open('ninja.txt','r+') as cn:
cn.write("New_Content")

You can also try this code with Online Python Compiler
Run Code
The above code will overwrite the old text with the new one. Hence, the output of the code will be:
New_Contentas - Become a Ninja
Clearly, the first 11 characters of the old content are overwritten with the new text.
Using w+ mode:
with open('ninja.txt','w+') as cn:
cn.write("New_Content")

You can also try this code with Online Python Compiler
Run Code
In w+ mode, the old content is deleted, and the file gets saved with the new text. So the output would be just the new text i.e. New_Content
Also see, Convert String to List Python
Frequently Asked Questions
What are the six access modes in python?
The six access modes in python are read Only (‘r’), read and write (‘r+’), write Only (‘w’), write and read (‘w+’), append Only (‘a’), append and read (‘a+’).
What is the use of r+ access mode?
The r+ or Read and Write mode helps in reading and writing the file. The pointer of the r+ access mode is located at the start of the file.
Which access modes show error messages in Python?
The r and r+ access modes show an Input/output error message when the file does not exist.
What is the difference between w and w+?
The w or Write mode opens the file for writing while w+ or writing and reading mode opens the file for writing as well as reading.
Conclusion
In this article, we have discussed the difference between r+ and w+ in python. We have also seen how we can perform the read and write operations using these access modes. To know more about file handling in python, you can refer to the below mentioned articles:
I hope this article has helped you in understanding a bit more about the differences between r+ and w+ in python. If this article helped you in any way, then you can read more such articles on our platform, Coding Ninjas Studio. You will find articles on almost every topic on our platform. Also, for cracking good product-based companies, you can practise coding questions at Coding Ninjas. For interview preparations, you can read the Interview Experiences of popular companies.
Happy Coding!!