Files, CSV and JSON lesson plan
Build the flight computer of a weather balloon: read and write files without losing them, log a live sensor to CSV, keep settings in JSON, organize folders, survive every disk error, and ship a sensor log tool.
Objectives and materials
By the end, students can
- Read a flight plan, then write a log that survives a power cut.
- Rows of readings in CSV, settings in JSON.
- Organize the files, and keep going when the disk says no.
- A sensor log tool for launch day.
Materials for the whole unit
- Pressure, humidity and temperature, BME280
- Raspberry Pi Zero 2 W
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Text files
Read a flight plan, then write a log that survives a power cut.
| Lesson | Time | What students do |
|---|---|---|
| Reading filesRead a balloon's flight plan from a text file: the whole file, its lines and their hidden newlines, a loop that skips comments, a key: value parser, and the bytes underneath the text. | 45 min |
|
| Writing safelyWrite a flight log without losing it: "w" that wipes the file, "a" that appends, newlines you have to add, print to a file, and an atomic save with os.replace that survives a power cut. | 45 min |
|
Materials
- Pressure, humidity and temperature, BME280
- Raspberry Pi Zero 2 W
Check for understanding
Each student finishes "Writing safely" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Survive a power cut".
CSV and JSON
Rows of readings in CSV, settings in JSON.
| Lesson | Time | What students do |
|---|---|---|
| CSV in and outRead a recorded balloon flight with DictReader, turn pressure into altitude, log the live BME280 to a CSV with csv.writer, append with a header only once, and see how commas and quotes inside values survive. | 45 min |
|
| JSON configKeep a flight computer's settings in config.json: load it, fill gaps from defaults, validate it with clear messages, save it back atomically with indent and sort_keys, and learn what JSON changes on the way through. | 45 min |
|
Materials
- Pressure, humidity and temperature, BME280
- Raspberry Pi Zero 2 W
Check for understanding
Each student finishes "JSON config" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "What JSON changes".
Folders and failures
Organize the files, and keep going when the disk says no.
| Lesson | Time | What students do |
|---|---|---|
| Paths and foldersOrganize a flight computer's files: the working folder, folders by date with makedirs, numbered log names that never overwrite, pathlib's Path objects, and a walk over the whole tree with sizes. | 45 min |
|
| Errors on diskMake a flight computer that keeps going when files go wrong: a missing config, a config cut off by a power failure, folders where files should be, bad rows in a CSV, try/except/else/finally, and the OSError family by errno. | 45 min |
|
Materials
- Pressure, humidity and temperature, BME280
- Raspberry Pi Zero 2 W
Check for understanding
Each student finishes "Errors on disk" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "The OSError family".
Project
A sensor log tool for launch day.
| Lesson | Time | What students do |
|---|---|---|
| Project: a sensor log toolSkylark's ground tool, from everything in the course: a config with defaults, numbered CSV logs of the live BME280 in a folder, a list of logs with rows and sizes, a summary saved as JSON atomically, and temperature alarms from the config. | 45 min |
|
Materials
- Pressure, humidity and temperature, BME280
- Raspberry Pi Zero 2 W
Check for understanding
Each student finishes "Project: a sensor log tool" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Alarms from the config".
Teach this unit as a class
Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.