Files, Data Formats, and Errors
Read and write portable files, exchange structured data, and make failures explicit without losing information.
Functions, imports, and basic Python syntax.
Read and write portable text files, parse JSON and CSV data safely, and handle expected failures (missing files, malformed data) precisely with try/except blocks.
Read in order, or jump to what you need.
Every lesson builds on the one before it. Open any lesson to read the explanation, run the examples, and try the practice challenges.
- 05.01Paths and Text Files4 min
Treat file locations, text encodings, and resource lifetimes as deliberate parts of your program.
- 05.02Text file workflows: rewrite, censor, and compare4 min
Read files line by line, rewrite them without corrupting the original, and verify copy, rename, and search operations.
- 05.03JSON and CSV4 min
Turn external representations into validated Python values and export data without breaking its structure.
- 05.04Exceptions and Validation4 min
Give failures useful meaning and recover only when your program knows what recovery should do.
- 05.05Context Managers4 min
Use with to make resource ownership and cleanup reliable on both success and failure.
Ready to move on?
Test valid input, missing files, empty files, and malformed input; explain exactly which cases should succeed and which should fail with clear error messages.
Open the module self-check