|
| 1 | +# Walkthrough: First File Reader |
| 2 | + |
| 3 | +> This guide walks through the **thinking process** for building this project. |
| 4 | +> It does NOT give you the complete solution. For that, see [SOLUTION.md](./SOLUTION.md). |
| 5 | +
|
| 6 | +## Before reading this |
| 7 | + |
| 8 | +**Try the project yourself first.** Spend at least 20 minutes. |
| 9 | +If you have not tried yet, close this file and open the [project README](./README.md). |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Understanding the problem |
| 14 | + |
| 15 | +You need to build a program that reads a text file, displays its contents with line numbers, and prints a summary (line count, word count, character count, non-empty lines). The program asks the user for a file path and handles the case where the file does not exist. |
| 16 | + |
| 17 | +This is your first encounter with **file I/O** -- reading data from a file on disk instead of from user input. |
| 18 | + |
| 19 | +## Planning before code |
| 20 | + |
| 21 | +```mermaid |
| 22 | +flowchart TD |
| 23 | + A[User enters file path] --> B{File exists?} |
| 24 | + B -->|No| C[Print error message] |
| 25 | + B -->|Yes| D[read_file_lines: read all lines] |
| 26 | + D --> E[format_with_line_numbers: add numbers] |
| 27 | + D --> F[file_summary: count words/lines/chars] |
| 28 | + E --> G[Display numbered contents] |
| 29 | + F --> H[Display summary stats] |
| 30 | +``` |
| 31 | + |
| 32 | +Three independent functions, plus error handling: |
| 33 | + |
| 34 | +1. **read_file_lines()** -- open a file, return a list of lines |
| 35 | +2. **format_with_line_numbers()** -- take lines, add line numbers |
| 36 | +3. **file_summary()** -- count lines, words, characters |
| 37 | +4. **Main block** -- ask for path, handle errors, display output |
| 38 | + |
| 39 | +## Step 1: Reading lines from a file |
| 40 | + |
| 41 | +The fundamental pattern for reading a file in Python: |
| 42 | + |
| 43 | +```python |
| 44 | +def read_file_lines(filepath: str) -> list: |
| 45 | + with open(filepath, encoding="utf-8") as f: |
| 46 | + return f.read().splitlines() |
| 47 | +``` |
| 48 | + |
| 49 | +Two important things here: |
| 50 | + |
| 51 | +- **`with open(...) as f`** opens the file and guarantees it gets closed when you are done, even if an error occurs. Always use `with` for files. |
| 52 | +- **`.splitlines()`** splits the text on newline characters and returns a list. Unlike `.split("\n")`, it does not leave an extra empty string at the end if the file ends with a newline. |
| 53 | + |
| 54 | +### Predict before you scroll |
| 55 | + |
| 56 | +If `sample_input.txt` contains three lines (one blank), how many items will the returned list have? Will the blank line be included or skipped? |
| 57 | + |
| 58 | +## Step 2: Adding line numbers |
| 59 | + |
| 60 | +You want output like: |
| 61 | + |
| 62 | +``` |
| 63 | + 1 | Welcome to your first file reader! |
| 64 | + 2 | |
| 65 | + 3 | This file has several lines of text. |
| 66 | +``` |
| 67 | + |
| 68 | +The `enumerate()` function gives you both the index and the value as you loop: |
| 69 | + |
| 70 | +```python |
| 71 | +def format_with_line_numbers(lines: list) -> str: |
| 72 | + if not lines: |
| 73 | + return "(empty file)" |
| 74 | + |
| 75 | + width = len(str(len(lines))) # how many digits for the biggest line number |
| 76 | + |
| 77 | + numbered = [] |
| 78 | + for i, line in enumerate(lines, start=1): |
| 79 | + numbered.append(f" {i:>{width}} | {line}") |
| 80 | + |
| 81 | + return "\n".join(numbered) |
| 82 | +``` |
| 83 | + |
| 84 | +The key trick is `f"{i:>{width}}"`. The `:>` means right-align, and `width` is how many characters wide to make the number. For a 100-line file, `width` would be 3, so line 1 displays as ` 1` and line 100 displays as `100`. |
| 85 | + |
| 86 | +### Predict before you scroll |
| 87 | + |
| 88 | +Why does the function handle the empty-lines case separately at the top? What would go wrong if `lines` were an empty list and you tried to calculate `width`? |
| 89 | + |
| 90 | +## Step 3: Building the summary |
| 91 | + |
| 92 | +The summary counts several things about the file: |
| 93 | + |
| 94 | +```python |
| 95 | +def file_summary(filepath: str, lines: list) -> dict: |
| 96 | + text = "\n".join(lines) |
| 97 | + word_count = len(text.split()) |
| 98 | + |
| 99 | + name = filepath.replace("\\", "/").split("/")[-1] |
| 100 | + |
| 101 | + return { |
| 102 | + "file_name": name, |
| 103 | + "lines": len(lines), |
| 104 | + "words": word_count, |
| 105 | + "characters": len(text), |
| 106 | + "non_empty_lines": sum(1 for line in lines if line.strip()), |
| 107 | + } |
| 108 | +``` |
| 109 | + |
| 110 | +Notice the last line: `sum(1 for line in lines if line.strip())`. This is a **generator expression** that counts only lines that are not blank. `line.strip()` removes whitespace -- if what is left is an empty string, it is falsy, so the condition filters it out. |
| 111 | + |
| 112 | +## Step 4: Handling missing files |
| 113 | + |
| 114 | +In the main block, wrap the file reading in `try/except`: |
| 115 | + |
| 116 | +```python |
| 117 | +try: |
| 118 | + lines = read_file_lines(filepath) |
| 119 | +except FileNotFoundError: |
| 120 | + print(f" File not found: {filepath}") |
| 121 | +``` |
| 122 | + |
| 123 | +This prevents the program from crashing with an ugly traceback when the user types a wrong path. |
| 124 | + |
| 125 | +## Common mistakes |
| 126 | + |
| 127 | +| Mistake | Why it happens | How to fix | |
| 128 | +|---------|---------------|------------| |
| 129 | +| Using `f.readlines()` instead of `f.read().splitlines()` | Both read lines, but `readlines()` keeps the `\n` at the end of each line | Use `.read().splitlines()` for clean lines without trailing newlines | |
| 130 | +| Line numbers start at 0 | `enumerate()` defaults to `start=0` | Pass `start=1` to `enumerate()` | |
| 131 | +| Word count is wrong | Splitting on just `" "` misses tabs and multiple spaces | Use `.split()` with no argument -- it splits on any whitespace | |
| 132 | +| Crash on empty file | Dividing by zero or formatting empty data | Check `if not lines` at the start and return early | |
| 133 | + |
| 134 | +## Testing your solution |
| 135 | + |
| 136 | +Run the tests from the project directory: |
| 137 | + |
| 138 | +```bash |
| 139 | +pytest -q |
| 140 | +``` |
| 141 | + |
| 142 | +The five tests check: |
| 143 | +- Reading a simple 3-line file returns the correct lines |
| 144 | +- Reading a missing file raises `FileNotFoundError` |
| 145 | +- `format_with_line_numbers()` adds correct line numbers |
| 146 | +- An empty file produces the `"(empty file)"` message |
| 147 | +- `file_summary()` returns correct line and word counts |
| 148 | + |
| 149 | +## What to explore next |
| 150 | + |
| 151 | +1. Add a feature that asks the user for start and end line numbers, then displays only that range |
| 152 | +2. After showing the summary, ask "Read another file? (y/n):" and loop if yes |
0 commit comments