Chapter DFile handlingPage 1 of 8

File handling

Define the lab goal and success criteria

File handling moves durable data between program runs; explicit paths, encodings, and write destinations keep that handoff predictable.

~13 minLab goal

1Try it yourself

Code Lab

Data: tables & simple stats

Run the average, then print the top student name.

Before you start

Why this matters

Before running anything, predict one observable result from the case: a noisy labels.txt must become a lowercase, de-duplicated clean-labels.txt without overwriting the source. Write the prediction beside the command or code line that should cause it. This makes the session an experiment rather than a transcription exercise.

2Learn the idea

Read

Explain the chapter step

Begin by writing the success condition in observable terms. For this case, success is not familiarity with the vocabulary; it is producing a script that reads UTF-8 text, cleans non-empty labels, writes atomically to a separate path, and reports counts. Record the starting state so you can distinguish an improvement from a result that was already present.

On this page, the practical job is to state a measurable outcome before changing anything. The running case is a noisy labels.txt must become a lowercase, de-duplicated clean-labels.txt without overwriting the source.

Read

Run the working example

from pathlib import Path

source = Path("labels.txt")
target = Path("clean-labels.txt")
lines = source.read_text(encoding="utf-8").splitlines()
clean = list(dict.fromkeys(
    line.strip().lower() for line in lines if line.strip()
))
target.write_text("\n".join(clean) + "\n", encoding="utf-8")
print(f"read {len(lines)}; saved {len(clean)}")

Expected evidence:

read 5; saved 3

The output may include version-specific details such as hashes, paths, fitted thresholds, or final decimal places. Compare the structural facts described here rather than copying placeholders. If the structure differs, stop and inspect the earliest unexpected line.

Read

Read it line by line

  1. Path keeps path operations clearer than manual slash concatenation.
  2. read_text decodes bytes as UTF-8 and splitlines removes line endings.
  3. the generator normalizes values, while dict.fromkeys removes duplicates without changing first-seen order.
  4. write_text replaces the target and returns a byte count; the source path is never opened for writing.

These lines form one chain: UTF-8 lines that may contain blank space, duplicate labels, and mixed capitalization becomes one normalized label per line in a new file. Change only one input first. When several values change together, you cannot tell which change caused the new behavior.

Read

Common errors and fixes

  • First failure: FileNotFoundError includes the unresolved path; print source.resolve() to verify the current working directory. Re-run the smallest command that proves the repair.
  • Second failure: UnicodeDecodeError means the declared encoding does not match the bytes. Preserve the failing input as a test when it represents a realistic mistake.
  • Misleading success: PermissionError or a partial write needs a safe destination; critical workflows should write a temporary file and replace only after success. A clean-looking final line cannot cancel contradictory intermediate evidence.

When debugging, copy the exact error text and inspect names, paths, shapes, types, and versions. Explain the cause in one sentence before changing code. That discipline prevents a guessed repair from creating a second defect.

Read

Evidence for this stage

Use a fixture containing blanks, Cat, cat, and a non-ASCII label. Reopen the target and compare exact lines. Run the script twice and verify the second output is identical.

For the current explain step, save the smallest useful evidence: the relevant command, its output, and the input that produced it. Do not use a screenshot as the only record when text can be copied and searched. Keep generated artifacts separate from source inputs so rerunning the example does not destroy the evidence it is meant to evaluate.

Include a tiny sample input, expected output, and whether writing replaces an existing target. For large datasets, document a streaming variant using with source.open(...).

Read

Reflect on the result

Return to your opening prediction. Mark it correct or rewrite it with the condition you missed. Then explain the difference between a successful execution and a trustworthy result for this specific example.

Checking tutor…

Continue learning · glossary & guides
  1. Which line or command establishes the current step's most important fact?
  2. What output would reveal that FileNotFoundError includes the unresolved path?
  3. Can a new user reproduce a script that reads UTF-8 text, cleans non-empty labels, writes atomically to a separate path, and reports counts from the stated setup?