Chapter 8

Files and Small Programs

Read saved text, preserve its evidence while converting values, write and verify a derived report, and organize the workflow into a small program whose parts have clear roles.

Until now, most examples have placed their input directly in Python code. In this chapter, a small text file becomes the input boundary. We will inspect the exact strings Python reads before converting anything, so newlines, invalid rows, and source line numbers remain visible.

The same labelled measurement file continues through the chapter. We will distinguish text from its UTF-8 bytes, convert only the values that match the stated format, compute a report from the accepted rows, write that report, and read it back to verify the saved result.

Paths identify the input and output locations. Modules give reusable parsing and calculation functions a home. Counter and defaultdict shorten two familiar collection loops only after we verify that the shorter versions mean the same thing. The final lesson combines these parts into a small program and runs it twice.

After this chapter

  • Read a complete UTF-8 text file or iterate through its lines while preserving visible input evidence.
  • Distinguish strings from bytes and complete a strict UTF-8 encode, write, read, and decode round trip.
  • Parse labelled rows near the input boundary, retain line identity, and handle only the expected numeric conversion failure.
  • Choose replacement or append deliberately, write exact report text, and verify it by reading the file back.
  • Use pathlib and working-directory evidence to locate input and prepare only generated output locations.
  • Move reusable parsing and calculation into a module and keep imported names traceable to their source.
  • Replace familiar counting and grouping loops with Counter and defaultdict when their behavior is a deliberate fit.
  • Organize, run, repair, and rerun a small read–compute–write program with clear file roles.

Lessons

  1. 01
    Read Text at the Program Boundary

    Open UTF-8 text safely, compare complete reads with line iteration, and preserve the evidence present at the input boundary.

    3 exercises
  2. 02
    Distinguish Text from Bytes

    Distinguish strings from bytes, inspect byte indexing, and perform a strict UTF-8 round trip.

    3 exercises
  3. 03
    Convert, Compute, and Report

    Parse labelled text rows near the input boundary, preserve line identity, and compute a report from accepted values.

    3 exercises
  4. 04
    Write and Verify Output

    Write or append derived text deliberately and verify the exact saved contents by reading them back.

    3 exercises
  5. 05
    Use Paths Without Guessing

    Build paths with pathlib, distinguish input from generated output, and diagnose missing files from evidence.

    3 exercises
  6. 06
    Import Reusable Code

    Separate reusable definitions from a runner and use explicit local and standard-library imports.

    3 exercises
  7. 07
    Use Standard Library Collection Tools

    Use Counter and defaultdict for deliberate counting and grouping while keeping ordinary dictionaries as the baseline.

    3 exercises
  8. 08
    Organize a Small Read–Compute–Write Program

    Organize and rerun a small multi-file program that reads, parses, computes, writes, and verifies a report.

    3 exercises

Review and practice

  1. Review

    Review the complete read–convert–compute–write workflow and the decisions that keep it inspectable.

  2. Exercises

    Practice Chapter 8 through twelve cumulative prediction, diagnosis, implementation, and full-workflow exercises.

Optional and reference

  1. Local Python Environments

    Optional local setup for virtual environments, interpreter selection, and package installation before NumPy.

  2. Script Entry Points

    Reference Python's main function, __name__ guard, and python -m module execution pattern.

Chapter progress