Write and Verify Output
Choose whether an output file should be replaced or extended, write an exact report, and read it back before trusting the saved result.
The measurement program can now turn saved text into a useful result. That result disappears when the program ends unless we write it somewhere. Writing is only half of the job: we should read the saved report back before claiming that it contains what we intended.
Write Derived Results
The source rows include two north readings, two west readings, one café reading,
and one invalid east row. After reporting and skipping east, the program has
five accepted readings with a mean of 21.3.
A report should save that derived result and the rejected-row count rather than copy the input:
Mode "w" opens a text file for writing. If report.txt does not exist,
Python creates it. If it does exist, "w" replaces its previous content. That
replacement is useful for a report that should describe the latest complete
run.
write receives one string. The newline characters are explicit because
write does not add them for us. The final newline makes the saved text end as
three complete lines:
accepted: 5
rejected: 1
mean: 21.3
Q1. Choose a mode for the latest report
Each run should replace yesterday's summary with one complete current summary. Which mode fits that contract?
Select one choice, then check.
HintPicture the second run
The file should still contain one report after the program runs twice.
SolutionReplace the complete report
Use "w". It creates the file when needed and replaces old content with the
current complete report.
Read the Saved Report Back
A call to write finishing without an exception is useful evidence, but it is
not the same as inspecting the saved content. Open the file again in text read
mode:
We can compare the value read back with the exact value we meant to save:
assert saved_text == report_text
This check catches omissions such as a missing line or newline. It also keeps the three values distinct:
report_text -> text constructed by the program
report.txt -> stored file
saved_text -> text decoded from that file on read-back
Equality between saved_text and report_text verifies this small round trip.
It does not verify that the earlier calculation was mathematically correct;
the function tests from Chapter 4 still provide that evidence.
Q2. Verify an exact text round trip
Complete the read-back check so the program verifies all three report lines and their newline characters.
Editable Python
Ready to run.
HintCompare strings, not only line counts
Use report_file.read(), then compare the returned string directly with
report_text.
SolutionRead and compare the complete report
Append Only When History Is the Result
Mode "a" opens a text file for appending. New text is written after the
existing content rather than replacing it:
This is appropriate when each run is a new record in a history. It is not a
harmless substitute for "w". Running the append code twice creates two
lines. The program must include the newline that separates them.
Choose the mode from the result the file represents:
| File's meaning | Mode | Effect of a second run |
|---|---|---|
| latest complete report | "w" | replace the first report |
| history of runs | "a" | add another record |
Q3. Predict two appended runs
The following block runs twice against an initially absent file:
What will the file contain?
Select one choice, then check.
HintPreserve, then add
"a" writes after existing content, and the explicit newline completes each
record.
SolutionThe history contains two records
mean: 21.3
mean: 21.3
Append mode creates the file if needed, preserves its content, and adds one new line on each run.
Write a derived report with explicit text and line endings, choose "w" for
one latest result or "a" for a history, and read the file back before
treating persistence as verified. A correct filename can still point to the
wrong place, so the next lesson makes file locations visible.