What is the Difference between r+ and w+ in Python | Example

What is the Difference between r+ and w+ in Python | Example

What is the Difference between r+ and w+ in Python | Example

Python interview questions and answers

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.

Difference between r+ and w+ in Python

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.

Read/Write Example using ‘r+’ and ‘w+’ mode

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.

1. Reading file in ‘r+’ mode:

with open('sample.txt','r+') as fd:
    print(fd.read())

Output:

Welcome to CSEstack.org.
We are learning File Handling in Python.

2. Reading file in ‘w+’ mode

with open('sample.txt','w+') as fd:
    print(fd.read())

Output:

It deletes all the previous content from the file and prints nothing.

3. Write file in ‘r+’ mode

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.

4. Write file in ‘w+’ mode

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.

5. Opening file in ‘r+’ mode when it is not exist

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.

6. Opening file in ‘w+’ mode when it does not exist

with open('sample.txt','w+') as fd:
    pass

Output:

If the file is not exist, it creates new empty file.

What should you use?

If you understand the difference and above examples, you can make your discussion very easily. Here are some pointers.

  • If you don’t want to read the content from the file, use ‘w+’ mode for opening the file.
  • If you want to create a new file if it does not exist, use ‘w+’ mode for opening the file. Otherwise, use ‘r+’ mode for.
  • If you only want to perform reading operation, it is recommended to open a file in ‘r+’ mode.

This is simple explanation for difference between r+ and w+ in Python. If you have any doubt, write in the comment.

4 Comments

  1. 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)?

    f = open("students6.txt", "w+")  #1
    f.write("First Line\n")                  
    f.write("Second Line\n")
    f.write("Third Line\n")
    print(f.tell())
    f.seek(0)
    f.write("4 Line")
    f.seek(0)
    print(f.read(10))
    print(f.tell())
    f.write("5 Line")    #11 why is this added at the end and not added at the begining like 4  line
    f.seek(0)
    print("after adding:\n",f.read())
    f.close()
    

Leave a Reply

Your email address will not be published. Required fields are marked *