In this topic students learn to read data from a text file: open it, read it line by line or one line at a time, and close it.
Be able to read, comprehend, trace, adapt and create Python code that:
for loop.readline(), and reads a whole file with a
while loop that stops at the empty string.strip(), and converts a line to a
number with int()This chapter was written for Dodona from new material by the course author, after the rest of the course had been converted, so it has no deck of its own. The author’s Intermediate Python series has a File handling deck that covers both reading and writing files with different examples; the Writing files chapter shares it: on Google Slides. A PDF copy is kept on Dodona too.
This is the first chapter where a program’s data comes from somewhere other
than the code or the keyboard. The next chapter writes files; this one only
reads them, so every file is opened with "r".
The chapter shows two ways of reading a whole file, on purpose. for line in
my_file: is the short form and the one students should reach for; it is a
count controlled loop, one repetition per line. The while line != "": loop
over .readline() is the long form, and is condition controlled. It is the
same relationship as for and while in the two iteration chapters: the long
form makes every step visible (read before the loop, check, read again at the
end of the body), and forgetting the last step gives an infinite loop, exactly
as forgetting to change a counter does. The last make task asks for the long
form, so students have to write that loop out.
The reading pages cannot run code that opens a file, so file-reading examples
are shown with the file’s contents and their output instead of a Run button.
Every exercise’s sandbox (the To sandbox button under the editor) does have
its files, and the Modify exercise’s sandbox has singers.txt, the file the
predict and investigate tasks use, so that is where students can run those
examples for real.
open("singers.txt", "r") needs the full file name, .txt included, and its
result has to be stored in a variable to be of any use.line is "Kurt\n", not
"Kurt". print(line) therefore leaves a blank line after every line..strip() removes the newline (and any spaces) from both ends. It returns a
new string rather than changing the old one, like .lower()..readline() returns "" only at the end of the file. A blank line in the
middle of a file is "\n".int() turns a line into a number and ignores the newline,
so int("999\n") is 999; int("") and int("\n") are errors.if line == "John": and prints nothing, although John is in the file.
This is the single most useful thing in the chapter, so it is worth letting
students predict it wrong and then explaining it on the board, with repr if
it helps: print(repr(line)) shows the \n.FileNotFoundError, from the missing
.txt) and stop there miss the NameError behind it.open("file_name", "r")
looks for a file called file_name. The file name is in a variable, so it
goes into open without quotes.code == 999 without int() is always false. Students who compare
it as text instead, code == "999", fail on the newline; the test file
holding 0999 catches students who strip the line and compare text.readline. In the pass counter, a loop that never
reads the next line never reaches the empty string, and never ends.> instead of >=. A score of exactly 50 is a pass, and several of the
test files contain one.