Bird
Raised Fist0
Pythonprogramming~10 mins

Overwrite vs append behavior in Python - Visual Side-by-Side Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Overwrite vs append behavior
Start
Write data
Overwrite mode?
YesReplace existing content
File has new content only
Append mode?
YesAdd data to end
File has old + new content
End
This flow shows how writing to a file either replaces all content (overwrite) or adds new content at the end (append).
Execution Sample
Python
with open('file.txt', 'w') as f:
    f.write('Hello')
with open('file.txt', 'a') as f:
    f.write(' World')
This code first overwrites file.txt with 'Hello', then appends ' World' to it.
Execution Table
StepFile ModeActionFile Content After Action
1'w' (overwrite)Write 'Hello''Hello'
2'a' (append)Write ' World''Hello World'
3EndNo more writes'Hello World'
💡 Writing ends after append; file content is 'Hello World'
Variable Tracker
VariableStartAfter Step 1After Step 2Final
file_content'' (empty)'Hello''Hello World''Hello World'
Key Moments - 2 Insights
Why does the file content get replaced in step 1?
Because opening the file with mode 'w' clears existing content before writing, as shown in execution_table step 1.
Why does the file content grow in step 2 instead of replacing?
Because mode 'a' appends data to the end without deleting existing content, as seen in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the file content after step 1?
A'' (empty)
B'Hello'
C'Hello World'
D' World'
💡 Hint
Check the 'File Content After Action' column for step 1 in the execution_table.
At which step does the file content become 'Hello World'?
AStep 2
BStep 1
CStep 3
DNever
💡 Hint
Look at the file content after each step in the execution_table.
If we open the file with mode 'w' again after step 2 and write 'Bye', what will the file content be?
A'Hello WorldBye'
B'Hello Bye'
C'Bye'
D'Hello World'
💡 Hint
Recall that mode 'w' overwrites the entire file content as explained in key_moments and execution_table.
Concept Snapshot
File write modes:
- 'w' overwrites file content (clears old data)
- 'a' appends data to end (keeps old data)
Use 'w' to replace, 'a' to add without deleting
Opening file with 'w' resets content immediately
Appending adds new data after existing content
Full Transcript
This visual trace shows how writing to a file in Python behaves differently depending on the mode used. When opening a file with 'w' mode, the file content is cleared first, so writing replaces everything. When opening with 'a' mode, new data is added to the end, preserving existing content. The example code writes 'Hello' with overwrite mode, then adds ' World' with append mode. The execution table tracks file content changes step-by-step. Key moments clarify why overwrite clears content and append adds to it. The quiz tests understanding of file content after each step and what happens if overwrite is used again after appending.

Practice

(1/5)
1. What happens when you open a file in Python with mode 'w' and write data to it?
easy
A. The file content is replaced with the new data.
B. The new data is added at the end of the file.
C. The file is opened for reading only.
D. The file content is duplicated.

Solution

  1. Step 1: Understand file mode 'w'

    Opening a file with mode 'w' means write mode, which clears existing content.
  2. Step 2: Effect of writing in 'w' mode

    Writing data in 'w' mode overwrites any existing content with the new data.
  3. Final Answer:

    The file content is replaced with the new data. -> Option A
  4. Quick Check:

    Overwrite = Replace content [OK]
Hint: Mode 'w' always replaces file content [OK]
Common Mistakes:
  • Thinking 'w' appends data
  • Confusing 'w' with 'a' mode
  • Assuming 'w' opens file for reading
2. Which of the following is the correct way to open a file for appending text in Python?
easy
A. open('file.txt', 'w')
B. open('file.txt', 'r')
C. open('file.txt', 'a')
D. open('file.txt', 'x')

Solution

  1. Step 1: Identify append mode

    Mode 'a' opens the file for appending, adding data at the end without deleting existing content.
  2. Step 2: Check other modes

    'w' overwrites, 'r' reads only, 'x' creates new file and errors if exists.
  3. Final Answer:

    open('file.txt', 'a') -> Option C
  4. Quick Check:

    Append mode = 'a' [OK]
Hint: Use 'a' to add data without deleting old [OK]
Common Mistakes:
  • Using 'w' when append is needed
  • Confusing 'r' with append mode
  • Using 'x' which fails if file exists
3. What is the output of this code?
lines = ['one', 'two']
with open('test.txt', 'w') as f:
    for line in lines:
        f.write(line + '\n')
with open('test.txt', 'a') as f:
    f.write('three\n')
with open('test.txt') as f:
    print(f.read())
medium
A. SyntaxError
B. three\n
C. one\ntwo\n
D. one\ntwo\nthree\n

Solution

  1. Step 1: Write lines with 'w' mode

    The first block writes 'one' and 'two' each on new lines, overwriting any old content.
  2. Step 2: Append 'three' with 'a' mode

    The second block adds 'three' on a new line at the end without removing previous lines.
  3. Step 3: Read and print file content

    The last block reads all lines, so output is 'one\ntwo\nthree\n'.
  4. Final Answer:

    one\ntwo\nthree\n -> Option D
  5. Quick Check:

    Write 'w' then append 'a' = combined content [OK]
Hint: 'w' clears file, 'a' adds after existing content [OK]
Common Mistakes:
  • Assuming 'a' overwrites content
  • Forgetting newline characters
  • Expecting only last write to appear
4. This code tries to add a line to a file but does not work as expected:
with open('data.txt', 'w') as f:
    f.write('Hello\n')
with open('data.txt', 'w') as f:
    f.write('World\n')

What is the problem?
medium
A. The second write overwrites the first line.
B. The file is opened in read mode instead of write mode.
C. The file is not closed before second write.
D. The write method cannot be used twice on the same file.

Solution

  1. Step 1: Analyze first write

    The first block writes 'Hello' and creates or overwrites the file.
  2. Step 2: Analyze second write with 'w'

    The second block opens the file again in 'w' mode, which clears previous content, then writes 'World'.
  3. Final Answer:

    The second write overwrites the first line. -> Option A
  4. Quick Check:

    Opening with 'w' overwrites content [OK]
Hint: Opening with 'w' erases old content [OK]
Common Mistakes:
  • Thinking file appends automatically
  • Believing file must be closed manually
  • Assuming write() can only be called once
5. You want to keep a log of events in a file without losing old logs. Which code snippet correctly appends new logs without overwriting existing ones?
medium
A. with open('log.txt', 'w') as f: f.write('New event\n')
B. with open('log.txt', 'a') as f: f.write('New event\n')
C. with open('log.txt', 'r') as f: f.write('New event\n')
D. with open('log.txt', 'x') as f: f.write('New event\n')

Solution

  1. Step 1: Understand log file needs

    Logs should be added without deleting old entries, so appending is needed.
  2. Step 2: Choose correct mode

    Mode 'a' appends data; 'w' overwrites, 'r' is read-only, 'x' creates new file and errors if exists.
  3. Final Answer:

    with open('log.txt', 'a') as f: f.write('New event\n') -> Option B
  4. Quick Check:

    Append mode 'a' preserves old data [OK]
Hint: Use 'a' mode to add without deleting old logs [OK]
Common Mistakes:
  • Using 'w' and losing old logs
  • Trying to write in 'r' mode
  • Using 'x' which fails if file exists