Python Program to open a file in read-write mode with truncating file


In Python, we can open a file in read-write mode by truncating the file by opening the file in w+ mode. Truncating a file refers to deleting the existing content of the file before opening the file. In this article, we will discuss how we can open the file in the read-write mode by truncating the file.

What is the w+ mode

The w+ mode in Python is used to open the file in read-write mode with file truncation. When the file is opened in w+ mode it allows us to read and write data in the file. If the file does not exist w+ mode creates a new file and opens it.

Syntax

open(‘filename’,’w+’)

The open methods above take the file name and the mode in which we want the file to be opened. The w+ mode indicates that the file should be opened in read-write mode with file truncation.

Example 1: Writing data to file using w+ mode

In the below example, we first open the file by using w+ mode in the open() function. We can write data to the file using the write() method and read the contents of the file by bringing the pointer to the start of the file and then reading the complete file.

# Open a file in read-write mode with truncating
with open('example.txt', 'w+') as file:
  
   # Write data to the file
   file.write('Hello, World!')
    
   # Move the file pointer to the beginning of the file
   file.seek(0)
    
   # Read the contents of the file
   contents = file.read()
    
   # Print the contents of the file
   print(contents)

Output

Hello, World!

Example 2: Rewriting data to file using w+ mode

If we again open the same file in w+ mode and write a new message, let's say “This is testing file truncation” then the output will be only the new message when the contents of the file are read and printed.

# Open a file in read-write mode with truncating
with open('example.txt', 'w+') as file:
  
   # Write data to the file
   file.write('This is testing file truncation!')
    
   # Move the file pointer to the beginning of the file
   file.seek(0)
    
   # Read the contents of the file
   contents = file.read()
    
   # Print the contents of the file
   print(contents)

Output

This is testing file truncation!

The above two examples demonstrate that the file gets truncated when opened in w+ mode. When we run the above program the example.txt file is truncated first i.e the contents of the example.txt file are deleted and new data is written to the file.

Example 3: Reading and writing data to a file using w+ mode

In the below example, we first open the file example.txt in w+ mode and read the contents of the file. As we open it in w+ mode it first truncates the file i.e the data/content of the file is cleared and the file is empty. So after reading the file it outputs an empty string. Then we write some content to the file using the write() method and then again read the file and print the contents of the file.

# Open a file in read-write mode with truncating
with open("example.txt", "w+") as file:

# Move the file pointer to the beginning of the file
   file.seek(0)

# Print the contents of the file
   print(file.read())

# Write data to the file
   file.write("This is a new message.\n")

# Move the file pointer to the beginning of the file
   file.seek(0)

# Print the contents of the file
   print(file.read())

Output

This is a new message.

Conclusion

In this article, we discussed how we can open a file in read-write mode with file truncation using the w+ mode of opening the file. The w+ mode first clears the contents of the file and then opens the file for reading or writing new contents to it. The w+ mode can be useful when working with an empty file to write new data every time.

Updated on: 17-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements