Card 15: Files
Module 6: Advanced Techniques
1 The Hook
Variables are like RAM (Short-term memory). Turn off the power, they vanish.
Files are like the Hard Drive (Long-term memory). Save now, load next week.
Modes: "w" (Write/Overwrite) vs "a" (Append/Add).
2 Worked Example
Saving a high score.
Reading Back
Reading Line-by-Line (The Loop)
PREDICT
You run this code. What happens to "notes.txt"?
f = open("notes.txt", "w")
f.write("Buy Bread")
f.close()
Input Required
Interactive Editor
🥉 Guest Book
1. Change the filename to "guest.txt".
2. Change the message to write YOUR name (e.g. "James").
3. Run and check the Virtual Disk below.
🥈 Data Wipe Disaster
This logger keeps deleting old data because it uses "w" (Write/Overwrite) mode!
1. Change "w" to "a" (Append).
2. Run it TWICE. You should see "Log Entry" appear twice in the Virtual Disk.
🥇 Chat Logger
1. Ask user for a msg.
2. Open "chat.txt" in Append mode.
3. Write the message + "\n" (New Line).
4. Close file.