Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to open a file in the read-write mode without truncating the file
In Python, we can open a file in read-write mode without truncating it by using the a+ mode. Truncating a file refers to deleting its existing content before opening. This article demonstrates how to preserve existing data while still being able to read and write to files.
What is a+ Mode?
The a+ mode opens a file for both reading and writing without truncating existing content. New data is appended to the end of the file, preserving all original content. The file pointer starts at the end for writing operations.
Syntax
open('filename', 'a+')
The open() function takes the filename and mode as parameters. The a+ mode indicates read-write access without truncation.
Example 1: Writing Data Using a+ Mode
Here we create a file with initial content, then read it back ?
# Create/write to file in a+ mode
with open("example.txt", "w") as f:
f.write("Original content.\n")
# Open in a+ mode and append new content
with open("example.txt", "a+") as file:
file.write("This is a test message.\n")
file.seek(0) # Move pointer to start
content = file.read()
print(content)
Original content. This is a test message.
Example 2: Demonstrating No Truncation
When we write additional content, the original data remains intact ?
# Add more content without truncating
with open("example.txt", "a+") as file:
file.write("Additional line without truncation.\n")
file.seek(0)
content = file.read()
print(content)
Original content. This is a test message. Additional line without truncation.
Example 3: Reading and Writing in One Operation
We can read existing content, then append new data in the same file operation ?
with open("example.txt", "a+") as file:
# Read existing content
file.seek(0)
existing_content = file.read()
print("Existing content:")
print(existing_content)
# Append new content
file.write("Final message added.\n")
# Read complete file
file.seek(0)
complete_content = file.read()
print("\nComplete file content:")
print(complete_content)
Existing content: Original content. This is a test message. Additional line without truncation. Complete file content: Original content. This is a test message. Additional line without truncation. Final message added.
Key Points About a+ Mode
| Operation | Behavior |
|---|---|
| File Opening | Creates file if it doesn't exist |
| Writing | Always appends to end of file |
| Reading | Requires seek(0) to read from start |
| Existing Data | Preserved (not truncated) |
Comparison with Other Modes
| Mode | Read | Write | Truncates |
|---|---|---|---|
| r+ | Yes | Yes | No |
| w+ | Yes | Yes | Yes |
| a+ | Yes | Yes (append only) | No |
Conclusion
The a+ mode is perfect for preserving existing file content while adding new data. Use seek(0) to read from the beginning, as the write pointer starts at the end of the file.
