CoursePython from Scratch to Intermediate · Module 8: Errors, Files and Debugging · part 54 of 75
Part 54 · Module 8: Errors, Files and Debugging

Lesson 2: Reading and writing files that outlast your program

16 min read·14 Oct 2026
Module 8 · Lesson 2 · About 45 minutes · Needs: Modules 1 to 7 (strings, loops, functions, pathlib.Path) and Lesson 1 (try and except)

The situation

Amira has Tom's typed-up notebook. "Can your program read it, and write down what is wrong with it?" Until now every value lived in a variable, and vanished when the session ended. A file stays on the disk after your program stops, and another program or person can open it.

This lesson follows one file, data/tom_notebook.txt, Tom's twelve lines from September: written, read back, and its faults recorded in a second file.

TOM_NOTEBOOK a variable write tom_notebook.txt on disk, stays read 12 lines in memory problems.txt faults kept variables vanish when the session ends; files stay
Tom's notebook goes from a variable (grey: gone when the session ends) to a file on disk (blue), back into 12 lines, and its faults into a second file (green).

The rest of this part opens with an account

Signing up is free and takes one email. Nothing to pay, and nothing renews.