Table of contents
1.
Introduction
2.
About r and r+
3.
About w and w+
4.
Difference between r+ and w+
5.
Operations on r+ and w+
5.1.
Read operation
5.2.
Write operation
6.
Frequently Asked Questions
6.1.
What are the six access modes in python?
6.2.
What is the use of r+ access mode?
6.3.
Which access modes show error messages in Python?
6.4.
What is the difference between w and w+?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference Between r+ and w+ in Python

Author Jay Dhoot
0 upvote

Introduction

Do you know that File handling is a significant part of programming?. The type of activity we can do on a file is defined by its access modes. There are six access modes present in Python.

Difference Between r+ and w+ in Python

In this article, we will discuss the critical difference between two essential access modes in Python, which are r+ and w+. Before understanding the difference between r+ and w+ in Python, let us first understand the difference between r and r+ and w and w+.

About r and r+

Read-only or r mode is the default mode of opening files. The pointer of r points to the start/beginning of the file, and this mode opens the files in read-only mode. It throws an Input/Output error if the file is not present. 

Read and Write, or r+ mode, is an extension to read mode. Read and write mode opens the file for both reading and writing. 

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:

  1. 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.
     
  2. 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.
     
  3. 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.
     
  4. 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.
     
  5. 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!!

Live masterclass