← Python Coding Track

Module 06BeginnerWeeks 3–4

File Handling

01

Learning outcomes

  • Read from and write to files
02

Core concepts

Opening/closing filesRead/write modesWorking with text and CSV files
03

Lessons

Module goal

Learner understands how to make data persist beyond a single run of a program — reading from and writing to files on disk.

Lesson 1 of 6 · lecture

Why file handling matters

Step 1 of 2

Every program written so far has a critical limitation: once it stops running, everything it stored disappears. The contact dictionary from Module 5 exists only while the script is executing — close the terminal, and it's gone. Real applications need data to survive between runs: a contact book should still have your contacts tomorrow; a repair tracker should remember every device logged last week.

File handling is how a program reads existing data from a file when it starts, and writes updated data back to a file before it ends — so information persists on the computer's storage, not just in temporary memory.

Teaching point

Frame this as the first step toward “real” software. Everything before this module lived and died within a single run. This is where programs start behaving like actual applications people rely on.

04

Guided labs

Lab — notes saver and loader

  1. 1.Create file_handling.py
  2. 2.Write a function save_note(note) that appends the given note (plus a newline) to notes.txt
  3. 3.Write a function load_notes() that opens notes.txt in read mode and prints every line inside it
  4. 4.Call save_note() two or three times with different messages
  5. 5.Call load_notes() afterwards to confirm all notes were saved and can be read back
def save_note(note):
    with open("notes.txt", "a") as file:
        file.write(note + "\n")

def load_notes():
    with open("notes.txt", "r") as file:
        for line in file:
            print(line.strip())

save_note("Picked up iPhone 12 for screen repair.")
save_note("Ordered replacement part for Samsung S21.")
save_note("Completed Pixel 6 battery replacement.")

print("\n--- All Notes ---")
load_notes()

Teaching point

Have the learner run the script twice and notice the notes list keeps growing — because "a" mode preserves what was already there. Ask them to predict what would happen with "w" instead, then let them test it.

Success criteria

notes.txt exists on disk after running the script, contains all saved notes on separate lines, and running the script again adds to (rather than replaces) the existing notes.

05

Knowledge Check

Question 1 of 6

What's the difference between "w" mode and "a" mode when opening a file?

06

What you read

  • File modes (r, w, a)
  • Using `with` for safe file handling
  • Basics of CSV structure
07

What you understand

Key takeaways

  • Programs need to persist data beyond runtime
08

Hands-on lab

Lab tasks

  1. 1.Build a script that saves and loads notes from a text file
09

Knowledge check

  • Identify correct file mode for a task
  • Spot unsafe file-handling code

Write your answers in your own notes before moving on — explaining a concept in your own words is the fastest way to find the gaps.

10

Mark progress