0%

Reading and Writing Files · practice

Write a Fresh Report

Printing proves what one run knew. A report file lets the result outlive the process.

This lesson adds two separate operations:

  1. build_report(books) turns Python values into one .

  2. write_report(path, text) stores that exact string.

Keeping them apart makes both easier to reason about. Report formatting needs no filesystem, and file writing needs no knowledge of books.

Build the whole text first

For any supplied , return this shape with one final newline:

Reading report
==============
Books: 2
- Unseen title one
- Unseen title two

Start a lines list with the heading and count. Append one bullet per book, then return "\n".join(lines) + "\n".

The final newline is small but deliberate. Text files produced by tools conventionally end with one, which keeps the next shell prompt and later appended text from landing on the last content line.

"w" means this file is a snapshot

Open the supplied report path like this:

with open(path, "w", encoding="utf-8") as handle:
    handle.write(text)

Mode "w" creates the file if it is missing. If it already exists, opening it in this mode empties it before the new text is written.

You can watch that happen:

Try it

Three lines went in, then one line went in, and one line is what remains. The second open did not add to the file or write over the first few characters. It emptied it.

That self-contained example writes a scratch demo.txt where the inline code runs. It does not create the chapter project’s report. In your project, keep the intended output path explicit so you can tell a current report from an old experiment.

That sounds destructive because it is. The important question is whether replacement matches the artifact’s meaning. reading_report.txt promises the current view of data/books.txt, not a historical archive. Leaving yesterday’s extra lines underneath today’s shorter report would be wrong, so replacement is the safe choice here.

Use "w" only after deciding that the whole file is replaceable. Python does not ask for confirmation.

Connect it to main

After the existing error handlers:

  • build the report from books;

  • write it to REPORT_PATH;

  • print Wrote reading_report.txt for N books. using the actual path and count.

Do not append history yet. The next lesson gives that different artifact a different mode.

Why is mode "w" appropriate for reading_report.txt?

Before you submit, test the replacement yourself. Run the program once, open reading_report.txt, and add some junk lines to the end by hand. Run it again. The junk should be gone, because "w" emptied the file first. If it is still there, your writer is appending.

Task

Implement the fresh report path.

  • build_report(books) must return the documented heading, actual count, one bullet per supplied title, and one final newline.

  • write_report(path, text) must use a with statement, explicit mode "w", UTF-8, and write the supplied text.

  • On successful input, main() must build and write REPORT_PATH, then print Wrote reading_report.txt for N books.

  • Keep the two existing narrow file-error handlers.

Do not append history yet.