Stage 6 · Lesson 3 of 8
Files, JSON and pathlib
JSON in Python is the job of the json module. It turns JSON text into dictionaries and lists, and back again. json.load(f) reads from an open file and json.dump(data, f, indent=2) writes to one.
pathlib.Path makes file paths work on every , as in Path("data") / "ledger.json". Path.exists() lets you create a starter file on the first run. For safely, open them with encoding="utf-8".
Validate what you load: data["amount"] raises KeyError when the key is missing, and "12.50" is a string, not a float. For tables of data, the csv module handles quoted values that a plain split(",") gets wrong. Keep loading and saving in their own , so the core logic receives plain dictionaries.
A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it
Resources for this stage
Start with· follow one of these
- GitHub repoFreeProject Based Learning
Community maintained · Beginner
Follow one guided build in your language, then build your own without a guide
- BookFreeAutomate the Boring Stuff with Python
Al Sweigart · Beginner
Free book of practical scripts: files, spreadsheets, web scraping and email
Keep handy
Also worth your time
How to present each project: the problem, stack, challenges and results
- Build Your Own XGo deeper
When you want a harder build: a shell, a key-value store or a tiny Git, from scratch