This module teaches safe text-file reading and writing with beginner-friendly patterns.
- Difficulty: Intermediate.
- Estimated Time: 30-45 minutes.
- Prerequisites:
01-foundations/types-and-io,01-foundations/strings. - Cross-Language Lens: Compare text-file APIs, line parsing, and error handling styles while keeping the workflow conceptually identical.
python example/main.py- Reading files line by line with context managers.
- Writing summary files safely with explicit output paths.
- Parsing
name scorerows with split and integer conversion. - Skipping malformed rows while keeping the program stable.
- Assuming input files always exist.
- Parsing rows without checking token count first.
- Letting one malformed row terminate the full run.
- Python makes file processing compact, so the comparison with C++ and Go highlights how little ceremony is required to read and transform text.
- Compared with C#, the code often becomes shorter, but validation discipline matters just as much.
- The key lesson is that concise I/O does not remove the need for defensive parsing.
- exercises/01.py: copy lines from one file to another with line numbers.
- exercises/02.py: parse
name scorerows, count invalid rows, and compute average.
- exercises/01.py
- Input: source file path and destination file path.
- Output: destination file with numbered lines (
1: ...,2: ...). - Edge cases: missing source file; empty source file.
- exercises/02.py
- Input: file path with rows in the format
name score. - Output: valid row count, invalid row count, and average score.
- Edge cases: malformed rows; file with no valid rows.
- I can open files safely and handle missing paths.
- I can parse and validate simple text rows.
- I can skip malformed data without aborting the run.
- I completed exercises/01.py.
- I completed exercises/02.py.