Appending Data to Files: Mode 'a'
Write mode ('w') opens a file for writing and immediately truncates (erases) any existing content in the file.
The Dangerous First Moment
Suppose notes.txt contains important data from yesterday. You run a Python script that opens the file in write mode to save today's notes. Before the script writes even one character, what happens to yesterday's data? The answer depends on the mode: opening an existing file with 'w' immediately erases its existing content.
What do you think happens?
If notes.txt already contains yesterday's notes, what will the file contain immediately after Python opens it with open('notes.txt', 'w')?
Reveal answer
Answer: The file is empty
Truncation happens at the instant the file is opened in 'w' mode, before any new data is written.
Opening in Write Mode
The second argument, 'w', selects write mode. Python opens the file for writing and immediately truncates it. Truncation means that any existing content is erased. This occurs before the program writes new data, so simply opening the file is enough to remove the old contents.
New Files and Existing Files
| Starting condition | Result of opening with 'w' |
|---|---|
| The file does not exist | Python creates the file |
| The file already exists | Python opens it and permanently removes its old data |
Write mode is useful when the intended result is a fresh file. For example, it can be used when generating a report, creating a configuration file, or saving computation output where the previous contents are no longer needed. If the file is new, Python creates it. If it already exists, its old contents are permanently removed.
Write Versus Append
Append mode, written as 'a', is the safer choice when existing content must remain. It preserves what is already in the file and positions new data at the end. Write mode, written as 'w', does the opposite with existing content: it clears the file so that new content can replace it.
| Mode | Existing content | Position of new data | Use when |
|---|---|---|---|
| 'w' | Erased when the file is opened | The file starts fresh | You want to replace the file's contents |
| 'a' | Preserved | At the end of the existing content | You want to add data without losing existing content |
The key difference between write mode and append mode is data preservation.
The two calls express different intentions. The first selects a mode that clears existing content. The second selects a mode that preserves existing content and adds new data after it. Choosing between them is therefore a data-preservation decision, not merely a spelling difference.
A File-State Prediction
Opening an Existing File with 'w'
Assume output.txt already contains data from a previous session. Predict the state of output.txt immediately after this statement runs: file = open("output.txt", "w")
Identify the mode: The mode is 'w', which opens the file for writing.
Check whether old content exists: The file is assumed to exist and already contain data.
Apply truncation: Opening an existing file in write mode immediately erases its existing content.
Stop before writing: No new data has been written yet, so the file is empty at this point.
Immediately after the open call, output.txt exists but contains no old data. The previous contents have already been removed.
Mistakes to Avoid
Assuming truncation happens only when new text is written
The old content is erased at the instant the file is opened.
Fix:
Treat the open call itself as the point at which existing data is removed.Using 'w' when the goal is to keep a file's history
Write mode clears yesterday's notes instead of preserving them.
Fix:
Use append mode, 'a', when new data must be added after existing content.Thinking 'w' and 'a' differ only in where the cursor starts
The modes have different data-preservation behavior: 'w' erases existing content, while 'a' preserves it.
Fix:
Choose 'w' to replace contents and 'a' to preserve contents while adding at the end.Opening the wrong filename in write mode
Existing data in that file is permanently removed.
Fix:
Verify the filename and use append mode if the existing data must be retained.
Choosing Intentionally
Decide what should happen to the existing file before selecting the mode. If the old content should be replaced entirely, choose 'w'. If it should remain and the new data should follow it, choose 'a'. Always verify the filename before opening a potentially important file in write mode.
A file named log.txt already contains records from earlier work. You need new records to remain alongside the old ones. Which mode should you choose, 'w' or 'a', and what will happen to the existing records when the file is opened?
Hints
- Ask whether the old records must be preserved.
- Recall which mode adds new data at the end.
Key Takeaways
- The 'w' mode opens a file for writing.
- When an existing file is opened in 'w' mode, Python immediately truncates it before any new data is written.
- If the file does not exist, Python creates it.
- The 'a' mode preserves existing content and adds new data at the end.
- Choose 'w' to replace a file deliberately and 'a' to add data without losing what is already there.
Key Takeaways
- Opening an existing file with 'w' immediately erases its contents.
- This erasure occurs before any new data is written.
- Python creates the file if it does not already exist.
- Append mode 'a' preserves existing data and adds new data at the end.
- The correct mode depends on whether the old file contents should be replaced or preserved.