- Explain context manager and file mode.
- Apply today's ideas in a small Python program.
- Inspect output and improve the program after a test run.
- context manager
- file mode
- Path
- encoding
Use a context manager so files close even if code fails. pathlib represents paths safely across operating systems; do not assume the current folder is a particular location.
Before coding, describe the input, processing, and output in ordinary language. Run the smallest possible version, then extend it one change at a time. Read the error message before asking for help.
from pathlib import Path
Path('notes.txt').write_text('Study Python\n', encoding='utf-8')Write and read a notes file.
Create a data directory only if it does not exist.
Save and load one student name from a text file.
A file is created and its content prints.
Organise your project with src, data, and tests folders.
- Official Python documentation
- Week resources
- Relevant retained examples in the repository, where referenced above