Reading and writing text files

About this lesson: Reading and writing text files

In short

Variables are lost when a program ends; files keep data. Write, append, then read.

Reading and writing text files

What you'll explore

Writing a file: Data that lasts
Variables are lost when a program ends; files keep data. Write, append, then read.
Reading line by line: Until end of file
Read one line at a time until EOF.
Where files live: Secondary storage
Files are saved on secondary storage such as SSDs, so they survive switching off.
Check yourself: Modes
A program must add new scores without deleting old ones.

Also in this lesson

  • Step by step
  • Watch out for these
  • More than the lesson
  • Stretch your brain
  • Try it yourself

Words to know

Text file
A file storing characters.
Append
Add to the end of a file.
EOF
End of file.
CSV
Comma-separated values.
Persistent
Kept after the program ends.

Sources

  • Python documentation: reading and writing files
  • Raspberry Pi Foundation, Isaac Computer Science: file handling
  • BBC Bitesize, GCSE Computer Science: file handling
  • RFC 4180: CSV format

An interactive lesson with a live experiment: move the sliders, press the buttons and answer quick questions. 4 short steps and a 8-question quiz. Part of the Family plan. Written and checked by Shalo Lab. Last updated 11 November 2026.

← Two-dimensional arrays · Standard algorithms: totals, max, min, average, counting →

All lessons in Term 1 · Computing · Age 15