Level 0 / Project 07 - First File Reader¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | Walkthrough | Quiz | Flashcards | Diagram | Browser |
Estimated time: 20 minutes
Focus¶
- opening and reading plain text safely
Why this project exists¶
Read a text file, display its contents with line numbers, and build a summary of line counts, word counts, and file metadata. This is your first hands-on practice with file I/O and Path objects.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
The program asks you to enter a file path, then displays its contents with line numbers.
Expected terminal output¶
=== File Reader ===
Enter a file path to read (e.g. data/sample_input.txt): data/sample_input.txt
=== Contents of sample_input.txt ===
1 | Welcome to your first file reader!
2 |
3 | This file has several lines of text.
=== Summary ===
Lines: 3 (2 non-empty)
Words: 14
Characters: 57
5 passed
Expected artifacts¶
- Passing tests
- Updated
notes.md
Checkpoint: Baseline code runs and all tests pass. Commit your work before continuing.
Alter it (required) — Extension¶
- Ask the user for a start and end line number to display only a range of the file.
- After showing the summary, ask "Read another file? (y/n): " and loop if yes.
- Re-run script and tests.
Break it (required) — Core¶
- Enter a file path that does not exist -- does it show a clear error or crash?
- Create an empty file and read it -- does
file_summary()crash or return zero counts? - Read a file with very long lines (1000+ characters) -- does line numbering still align correctly?
Fix it (required) — Core¶
- Ensure
read_file_lines()raisesFileNotFoundErrorwith the path in the message. - Handle the empty-file case by returning a special "(empty file)" message.
- Add a test for the empty-file edge case.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- Why does
format_with_line_numbers()right-justify the line numbers? - What does
path.read_text(encoding="utf-8")do differently fromopen(path).read()? - Why track non-empty lines separately in
file_summary()? - Where would file reading with metadata appear in real software (log viewers, code editors, diff tools)?
Mastery check¶
You can move on when you can: - run baseline without docs, - explain one core function line-by-line, - break and recover in one session, - keep tests passing after your change.
Related Concepts¶
| ← Prev | Home | Next → |
|---|---|---|