Handling File Mode Combinations in Python
In Python, file mode combinations determine how files are opened for reading, writing, or appending. The open()
function provides different modes such as 'r'
(read), 'w'
(write), 'a'
(append), and 'b'
(binary), which can be combined for specific use cases. This tutorial explains how to handle file mode combinations with examples.
Examples for File Mode Combinations
1. Using 'w+'
Mode (Write and Read)
The 'w+'
mode allows both writing and reading from a file. If the file exists, its contents are erased. If it doesn’t exist, a new file is created.
In this example, we will consider a file example.txt, open it in read mode, and print its content to output.

main.py
# Opening a file in 'w+' mode
with open("example.txt", "w+") as file:
file.write("Hello, Python!\n")
file.write("File handling in Python.")
# Moving the cursor to the beginning
file.seek(0)
# Reading the contents
content = file.read()
print("File Content:\n", content)
Explanation:
open("example.txt", "w+")
: Opens the file for reading and writing.file.write()
: Writes text to the file.file.seek(0)
: Moves the cursor to the beginning for reading.file.read()
: Reads the content written.
Output:


2. Using 'a+'
Mode (Append and Read)
The 'a+'
mode allows both appending and reading from a file. The cursor is positioned at the end, meaning new content is added without overwriting existing content.

main.py
# Opening a file in 'a+' mode
with open("example.txt", "a+") as file:
file.write("\nAppending new data!")
# Moving cursor to the beginning
file.seek(0)
# Reading entire content
content = file.read()
print("Updated File Content:\n", content)
Explanation:
open("example.txt", "a+")
: Opens the file for appending and reading.file.write("\nAppending new data!")
: Adds new content at the end.file.seek(0)
: Moves the cursor to the beginning to read the full content.file.read()
: Reads the updated file content.
Output:


3. Using 'rb'
Mode (Read in Binary)
The 'rb'
mode is used to read files in binary format, often required for handling images, PDFs, or encoded files.

main.py
# Opening a file in 'rb' mode
with open("example.txt", "rb") as file:
binary_data = file.read()
print("Binary Data:\n", binary_data)
Explanation:
open("example.txt", "rb")
: Opens the file in binary read mode.file.read()
: Reads the file as raw binary data.
Output:

4. Using 'r+'
Mode (Read and Write)
The 'r+'
mode allows reading and writing without truncating the file. The cursor starts at the beginning, so modifications can be done anywhere in the file.

main.py
# Opening a file in 'r+' mode
with open("example.txt", "r+") as file:
print("Before modification:\n", file.read())
# Moving cursor to the beginning
file.seek(0)
# Writing new content
file.write("Updated content")
# Moving cursor to the beginning to read updated file
file.seek(0)
new_content = file.read()
print("After modification:\n", new_content)
Explanation:
open("example.txt", "r+")
: Opens the file for both reading and writing.file.seek(0)
: Moves the cursor to the beginning.file.write("Updated content")
: Overwrites existing content.file.seek(0)
: Ensures the new content can be read.
Output:


Conclusion
In this tutorial, we have covered multiple file mode combinations for different file-handling tasks:
'w+'
: Writes and reads, truncating the file.'a+'
: Appends and reads without deleting content.'rb'
: Reads in binary format.'r+'
: Reads and writes without truncating.