text file: A file that stores encoded text; mode: The chosen way to use a file; sequential: Read items in order
55-minute lesson sequence
0–5 · Answer the retrieval alone. Compare one reason.
5–15 · Teacher models the example. Students predict each next step.
15–40 · Build, trace and test the main tasks. Work alone first; compare after a checkpoint.
40–50 · Check results. Explain a test and repair one error. Use the extension only if ready.
50–55 · Complete the exit alone. Save the evidence for the next lesson.
Try before notes
What are the values and types of "25" and 25?
Build the idea
A text file stores encoded characters. Reading a line returns a string, even when those characters look like a number. A newline may be part of the returned string. Keep representation separate from numeric meaning.
strip removes surrounding whitespace. int converts valid integer text to an integer. Convert before arithmetic, and state the input contract: these first examples assume nonblank valid integer lines.
Open a file with with so the context manager closes it when the block ends. Use r for reading. A relative filename is resolved from the program’s working directory, which may differ from the folder visible in an editor.
file bytes → decode → line string → strip → int → calculation
See the method
Use a new practice folder. Create example.txt with 20 on the first line and 26 on the second. Each line ends with a newline. Opening in r mode reads the file. strip removes surrounding whitespace. int changes valid integer text to an integer. The with block closes the file when the block ends. Predict: 20, then 26, then Total 46.
total = 0
with open("example.txt", "r", encoding="utf-8") as source:
for line in source:
number = int(line.strip())
print(number)
total = total + number
print("Total", total)
Predict then reveal
A line is "13\n". What are line.strip() and int(line.strip()), including types?
Reveal reasoning
line.strip() is "13", a string. int(line.strip()) is 13, an integer. Stripping whitespace alone does not convert the type.
A new demo file contains 7 and 11 on separate lines. Start total=0; add each converted number. Trace the total.
Reveal reasoning
Before reading: 0. After 7: 7. After 11: 18. Adding the raw line string to the integer total would raise TypeError; numeric conversion is needed.
Work one step together
Read the first task. Each learner predicts the first step privately. Compare the method, then complete one intermediate row or one code iteration together. Do not display a finished workbook answer.
Practice
Create readings.txt in your new practice folder. Put 18, 22, 27 and 25 on separate lines. Adapt the example to read it. Before running, write the four printed readings and the total. Keep the file for the week.
Your turn
Complete the remaining steps of task 1 alone, then the next tasks. Predict before running code. Save your own evidence before comparing.
Practice
Create readings.txt in your new practice folder. Put 18, 22, 27 and 25 on separate lines. Adapt the example to read it. Before running, write the four printed readings and the total. Keep the file for the week.
Build a second version that prints a line number beside each reading. Start numbering at 1. Show the output. Then explain why total + line is not the right calculation.
A student saves the file in another folder. The program cannot find readings.txt. Give a safe check before changing the program. Do not delete or overwrite any existing work.
Check and explain
A numeric-looking file line is still text until converted.
Point to the first step, row, condition or requirement that supports your answer. If results differ, find the first different state and repair one cause.
Show what you know
Why do we use with when opening the file?
HL challenge when ready
Use readline twice, then read. What will each call return for the four-line file?
Week 6 homework
Show working. Keep code and output. Stop at 25 minutes and mark where you reached.
Without running code first, predict the report for a new file containing 24, 25, 26, a blank line and 19. Use limit 25. Then, if a computer is available, test and compare. Otherwise trace the function on paper.
A check history already has three lines. A program appends one line each run. It runs twice. How many lines are there? What would change if it used w each time?
Write one test that catches > used instead of >= at limit 25. State the input and expected answer.
Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.
A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.
Prompt 1
Split a small sensor log into lines and print each with its line number.
No imports needed.
Hint and starter code
A log is a sequence of recorded events or readings; splitlines() removes line separators.
# A log is a sequence of recorded events or readings; splitlines() removes line separators.
log = "24\n27\n29\n"
for number, line in enumerate(log.splitlines(), start=1):
print(number, line)