Phase 1: Prepare
Step 1 Warm-up recall
From Module 5: what does a function with no return statement give back?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- read a text file line by line (Foundation);
- write a new file (Core);
- choose between r, w and a (Core).
Phase 2: Teach
Step 3 A familiar example
A clerk copies figures from a ledger into a report. The ledger is only read; the report is a new sheet. A program treats files the same way: read the source, write somewhere else.
Step 4 Clear explanation
A file keeps data after the program ends. Python opens it with open, and with closes it for you when the block ends.
with open("scores.txt", "r") as f:
for line in f:
print(line.strip())
with open("report.txt", "w") as out:
out.write("Done\n")Ann,70
Bo,85Each line comes with a hidden newline at the end, so strip() removes it. The mode matters a great deal:
| Mode | Meaning | Effect on an existing file |
|---|---|---|
| "r" | read | unchanged; error if missing |
| "w" | write | ERASED, then written |
| "a" | append | kept; new text added at the end |
Mode "w" erases an existing file the moment it is opened, before anything is written. Never open your only copy of the data for writing. Read the source, and write a different file name.
Step 5 Worked example (4 marks)
A file notes.txt holds the lines a and b. A program opens it with mode "w" and writes c. What does the file contain afterwards?
- Mode "w" erases the old content on opening. (1 mark)
- Only c is then written. (1 mark)
- The earlier lines a and b are lost. (1 mark)
- Answer: only c; mode "a" would have kept a and b. (1 mark)
Go deeper: the professor's notes
Read.
Erases first.
Adds at end.
Modes
| Mode | Old content |
|---|---|
| r | unchanged |
| w | erased |
| a | kept |
At work: A report file
You need to save results.
- Use a new file name.
- Open it with w.
What a careful practitioner decides. Write to a new file; keep the source as it was.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. Opening an existing file with mode "w":
Write mode starts empty.
2. To add to the end of a file, use mode:
Append.
3. The benefit of with open(...) is:
with closes the file.
4. A line read from a file ends with:
Use strip().
Phase 4: Close
Step 7 Recap
- with open(...) opens and closes safely.
- Lines end in a newline; strip removes it.
- Mode w erases; mode a appends; r only reads.
Step 8 Exit ticket (ungraded)
Write the lines of data.txt to the screen, one per line, without blank gaps.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write a program that copies one file to another, adding the word COPY as the first line.
In industry: Toy Story 2 almost deleted
In 1998 a delete command removed roughly 90 percent of the files for Toy Story 2 from the studio servers. The backup system had been failing without anyone noticing. The production recovered because a supervising technical director, Galyn Susman, had a copy of the film on her home computer.
Link to this lesson. A backup that has never been restored is a hope, not a backup; test the restore.
Source: The Next Web, 21 May 2012; The Wall Street Journal as reported by Daily Wire; Kottke, 2012
Common mistakes
| Mistake | Why it fails |
|---|---|
| Opening the only copy with "w" | Write to a new file name. |
| Forgetting strip | Blank lines or stray newlines appear. |
| Not using with | The file may stay open and unsaved. |
Limits. This lesson covers plain text files; other file types such as spreadsheets use extra tools.