Files and Paths
- open and with. The with statement opens a file and guarantees it closes, even when the block raises.
- Reading and writing. read() takes the whole file; iterating the file object yields lines one at a time without loading everything.
- pathlib. The pathlib module treats paths as objects: joining with / and asking exists() beats gluing strings together.
JSON, CSV and Serialisation
- JSON. The json module turns dicts and lists into text and back: the wire format of nearly every API.
- CSV. The csv module reads spreadsheet-shaped data row by row, handling quoting rules you should never hand-roll.
- Pickle caution. pickle serialises arbitrary Python objects, but loading a pickle runs code: never unpickle untrusted bytes.
Resource Management
- Context managers. Anything usable in a with statement is a context manager: it promises setup and guaranteed cleanup.
- Encoding. Text files carry an encoding; declaring utf-8 explicitly is what keeps accents from becoming mojibake.
- Atomic writes. Write to a temporary file and rename it into place: a crash mid-write then never leaves a half file.
Before and after this level
- Before: Level 7: Errors & Debugging: Exceptions, Debugging and Tracebacks, Designing Reliable Failure Handling.
- Next: Level 9: Iteration & Pythonic Design: Iterators and Generators, Context Managers, Decorators and Reusable Behaviour.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Python Level 8: Files & Data cover?
Files and Paths, JSON, CSV and Serialisation, Resource Management: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 7: Errors & Debugging, which covers Exceptions, Debugging and Tracebacks, Designing Reliable Failure Handling.