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.

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
← Two-dimensional arrays · Standard algorithms: totals, max, min, average, counting →