Source: https://mokxi.com/teachers/plans/python-files-and-data
Updated: 2026-11-19

Lesson plan

# 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.

4 chapters, 7 lessons, about 5 hours, on the Raspberry Pi 4 Model B. Every lesson runs in the browser, with nothing to install.

Start a class from this course

The unit

## 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.

Chapter 1. About 1 h 30 min

## Text files

Read a flight plan, then write a log that survives a power cut.

Lesson | Time | What students do

Reading files Read 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

- Open and read
- Lines and the hidden newline
- Loop over the lines
- Parse key: value
- Bytes underneath

Writing safely Write 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

- Write a file
- "w" wipes the file
- "a" appends
- print() into a file
- Survive a power cut

### 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".

Chapter 2. About 1 h 30 min

## CSV and JSON

Rows of readings in CSV, settings in JSON.

Lesson | Time | What students do

CSV in and out Read 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

- Read a flight
- Pressure into altitude
- Log the live sensor
- Append, header once
- Commas inside values

JSON config Keep 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

- Load config.json
- Defaults for what is missing
- Validate before you fly
- Save it back
- What JSON changes

### 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".

Chapter 3. About 1 h 30 min

## Folders and failures

Organize the files, and keep going when the disk says no.

Lesson | Time | What students do

Paths and folders Organize 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

- Where am I?
- Folders by date
- Names that never overwrite
- pathlib
- Walk the tree

Errors on disk Make 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

- The missing file
- A damaged file
- Folders where files should be
- Bad rows, else and finally
- The OSError family

### 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".

Chapter 4. About 45 min

## Project

A sensor log tool for launch day.

Lesson | Time | What students do

Project: a sensor log tool Skylark'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

- The tool starts
- log N
- list
- summary
- Alarms from the config

### 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".

Your class

## 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.

Start a class from this course See the demo class

More units

## Other lesson plans

Electronics basics 20 lessons, about 10 h. Embedded on the Arduino Uno 16 lessons, about 12 h. Embedded on the ESP32-C3 13 lessons, about 11 h. Embedded on the Raspberry Pi Pico 7 lessons, about 6 h. Build your own fight stick 5 lessons, about 4 h. Embedded on the STM32F411 6 lessons, about 6 h. C++ on a board 13 lessons, about 10 h. Digital logic and memory 4 lessons, about 4 h. Interactive devices on the Uno 9 lessons, about 8 h. From gates to a computer 23 lessons, about 16 h. Build a claw machine 8 lessons, about 6 h. Every unit All the courses as ready-made units, on the teachers page.
