1. Introduction: Operating Across Different File Modes
After mastering the fundamentals of opening, reading, writing, and closing files in Python, the next essential step is controlling how a file is opened and operated on.
When opening a file using Python's built-in open() function, you can pass a mode flag as a second argument. This flag explicitly informs the Python interpreter of your operational intent—whether you plan to read existing contents, write fresh data, or append new information to the end of the file.
2. Mode 1: Read Mode ('r')
To read data from an existing file, specify the 'r' flag when calling open().
- Behavior: Informs Python that the target file is being accessed strictly for reading.
- File pointer & safety: The file is opened safely without modifying or deleting its existing contents.
# 1. Opening demo.txt in Read mode using the 'r' flag
file = open("demo.txt", "r")
# 2. Reading and printing the contents
content = file.read()
print(content)
# 3. Safely closing the file
file.close()
Execution Walkthrough
- Executing this script reads the stored text from
demo.txt.
- In our example,
demo.txt contains the initial text lines: "This is a text" and "New line".
- Python reads and prints these contents to the screen, after which the file is properly closed.
3. Mode 2: Write Mode ('w') & The Overwrite Behavior
To write data into a file, open it using the 'w' flag.
- Behavior: Informs Python that you intend to write data into the target file.
- Confirmation of written data: When you call
file.write(), Python evaluates the string and confirms the exact number of characters written into the file (for example, confirming 21 characters written for a sentence).
- The overwrite effect: When an existing file is opened in
'w' mode, all previous data is completely overwritten and replaced by the newly written content.
# 1. Opening demo.txt in Write mode using the 'w' flag
file = open("demo.txt", "w")
# 2. Writing new text into the file
file.write("This is another text")
# 3. Closing the file
file.close()
Execution Walkthrough
- Writing "This is another text" writes 21 characters into the file.
- Once the file is closed and
demo.txt is reloaded on disk, the original contents ("This is a text" and "New line") are gone.
- The file now contains only the new string: "This is another text".
4. Mode 3: Append Mode ('a') — Preserving Historical Data
In many real-world scenarios—such as logging events, recording transaction histories, or appending user entries—you do not want previous data to be overwritten. Instead, you need incoming information added cleanly to the end of the existing content.
For this purpose, Python provides Append Mode, designated by the 'a' flag.
- Behavior: Python opens the file for writing, but instead of truncating or wiping the old content, it places new data directly after the old data.
- Combining with newline characters (\n): To ensure that the appended text begins on a fresh line rather than attaching to the end of the previous sentence, prepend a backslash-n (
\n) escape character before the text string.
# 1. Opening demo.txt in Append mode using the 'a' flag
file = open("demo.txt", "a")
# 2. Appending new text on a new line using '\n'
file.write("\nNew line added")
# 3. Closing the file
file.close()
Execution Walkthrough
- Python appends "\nNew line added", confirming that 16 characters have been written into the file.
- Calling
file.close() ensures the appended changes are saved to disk.
- When
demo.txt is reloaded, the previous text remains fully intact, and the new line is successfully added underneath it:
This is another text
New line added
5. Summary: Choosing the Right File Mode
| Mode Flag |
Name |
Primary Function |
Effect on Existing Data |
'r' |
Read |
Opens file for reading |
Content is preserved unchanged |
'w' |
Write |
Opens file for writing fresh data |
Overwrites / erases all previous content |
'a' |
Append |
Opens file for writing at the end |
Preserves old data and appends new content |