Sometimes you want to replace old data with new data, and sometimes you want to add new data without losing the old. Understanding overwrite and append helps you control this.
Saving a new version of a file and replacing the old one (overwrite).
Adding new messages to a chat log without deleting previous messages (append).
Updating a list of tasks by adding new tasks without losing existing ones (append).
Replacing a configuration file completely with new settings (overwrite).
Syntax
Python
withopen('filename.txt', 'w') as file:
file.write('new content') # Overwrites the filewithopen('filename.txt', 'a') as file:
file.write('more content') # Appends to the file
Mode 'w' means write and will erase existing content before writing.
Mode 'a' means append and will add new content at the end without erasing.
Examples
This creates or overwrites 'example.txt' with the word 'Hello'.
Python
withopen('example.txt', 'w') as f:
f.write('Hello')
This adds ' World' to the end of 'example.txt' without removing 'Hello'.
Python
withopen('example.txt', 'a') as f:
f.write(' World')
This overwrites the whole file with 'New start', removing previous content.
Python
withopen('example.txt', 'w') as f:
f.write('New start')
Sample Program
This program first writes 'Line 1' to the file, erasing anything before. Then it adds 'Line 2' and 'Line 3' without erasing. Finally, it reads and prints all lines.
Python
filename = 'testfile.txt'# Overwrite mode: write initial contentwithopen(filename, 'w') as file:
file.write('Line 1\n')
# Append mode: add more lineswithopen(filename, 'a') as file:
file.write('Line 2\n')
file.write('Line 3\n')
# Read and print the file contentwithopen(filename, 'r') as file:
content = file.read()
print(content)
OutputSuccess
Important Notes
Using 'w' mode deletes all old content before writing.
Using 'a' mode keeps old content and adds new data at the end.
Always close files or use 'with' to avoid errors and data loss.
Summary
Overwrite replaces old data completely.
Append adds new data after old data.
Choose mode based on whether you want to keep or replace existing content.
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
Step 1: Understand file mode 'w'
Opening a file with mode 'w' means write mode, which clears existing content.
Step 2: Effect of writing in 'w' mode
Writing data in 'w' mode overwrites any existing content with the new data.
Final Answer:
The file content is replaced with the new data. -> Option A
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
Step 1: Identify append mode
Mode 'a' opens the file for appending, adding data at the end without deleting existing content.
Step 2: Check other modes
'w' overwrites, 'r' reads only, 'x' creates new file and errors if exists.
Final Answer:
open('file.txt', 'a') -> Option C
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
Step 1: Write lines with 'w' mode
The first block writes 'one' and 'two' each on new lines, overwriting any old content.
Step 2: Append 'three' with 'a' mode
The second block adds 'three' on a new line at the end without removing previous lines.
Step 3: Read and print file content
The last block reads all lines, so output is 'one\ntwo\nthree\n'.
Final Answer:
one\ntwo\nthree\n -> Option D
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
Step 1: Analyze first write
The first block writes 'Hello' and creates or overwrites the file.
Step 2: Analyze second write with 'w'
The second block opens the file again in 'w' mode, which clears previous content, then writes 'World'.
Final Answer:
The second write overwrites the first line. -> Option A
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
Step 1: Understand log file needs
Logs should be added without deleting old entries, so appending is needed.
Step 2: Choose correct mode
Mode 'a' appends data; 'w' overwrites, 'r' is read-only, 'x' creates new file and errors if exists.
Final Answer:
with open('log.txt', 'a') as f:
f.write('New event\n') -> Option B
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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.