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


In Python, we can open a file in a read-write mode without truncating the file by opening the file in a+ 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 without truncating the file.

What is a+ mode

The a+ mode in Python is used to open the file in a read-write mode without truncating the file. When the file is opened in a+ mode it allows us to write new data at the end of the file without affecting the existing data in the file.

Syntax

open(‘filename’,’a+’)

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

Example 1: Writing data to file using a+ mode

In the below example, we first open a file “example.txt” in a+ mode and write a message using the write() method. After writing the message we bring the pointer to the start of the file by using seek() method. The contents of the file are then printed using the read() method.

with open("example.txt", "a+") as file:
   file.write("This is a test message.")
   file.seek(0)
   print(file.read())

Output

This is a test message.

Example 2: Rewriting data to file using a+ mode

Initially, the example.txt file was empty but now the message “This is a test message” is written in the file. Now if you run the above code again with a different message, let's say “This is testing truncation in the file”, it will print both the original message as well as the new message in the file.

with open("example.txt", "a+") as file:
   file.write("This is testing truncation in file.")
   file.seek(0)
   print(file.read())

Output

This is testing truncation in file.

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

In the below example, we open our example.txt file in a+ mode and bring our pointer to the start of the file using seek(0) method. We then read the contents of the file using the read() method and print it.

After reading the file's existing data now we can write new data to the file using the write method as a+ mode allows us to read as well as write on the file. Again we can read the whole content of the file by bringing the pointer to the start of the file and then printing the contents of the file.

with open("example.txt", "a+") as file:
   file.seek(0)
   print(file.read())
   file.write("This is a new message.\n")
   file.seek(0)
   print(file.read())

Output

This is a new message.

Conclusion

In this article, we discussed reading and writing in a file without truncating the file. The a+ mode opens the file in a read-write mode without truncating the file's existing data. When we write new data to the file and print the complete content of the file, it prints both the existing data and the new data. The a+ mode can be useful when working with files in Python.

Updated on: 17-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements