Read Text at the Program Boundary
See exactly what a text file gives a program: one string or a sequence of strings whose newline characters remain part of the input.
A measurement program becomes more useful when its input can outlive one run. Instead of placing every reading inside the source code, we can save the rows in a text file and let the program read them.
We will use a file named measurements.txt throughout this chapter:
north,18.5
east,not recorded
west,24.0
café,21.5
north,19.0
west,23.5
Each row contains a label, a comma, and reading text. One label contains é,
and one row does not contain a number. For now, the important point is simpler:
opening a text file gives Python strings. It does not interpret those strings as
labels or numbers.
Read the Complete File as Text
The ordinary way to open a text file is a with statement:
open connects the program to the file. The mode "r" means read, and
encoding="utf-8" states how stored data becomes Python text. The name file
refers to the open file while the indented block runs. file.read() returns the
complete contents as one string.
The with statement also closes the file when its block ends, including when
an error interrupts the block. Closing releases the operating-system resource
used for that open file. We will use this safe form as the ordinary pattern
rather than managing closure by hand.
Printing text makes the file look much as it does in an editor. Printing
repr(text) makes line endings visible:
print(repr(text))
'north,18.5\neast,not recorded\nwest,24.0\ncafé,21.5\nnorth,19.0\nwest,23.5\n'
Each \n is a newline character. The final \n records that the last row also
ends with a newline. It is part of the string even though an ordinary print
uses it to move to the next line.
Q1. Find the visible newlines
What does text.count("\n") return for the complete file shown above?
Compute it first, then check your number.
HintInspect the complete representation
The last row has a newline too. It appears immediately before the closing
quote in the repr output.
SolutionThere are six newline characters
The string has one \n after each of its six rows, so
text.count("\n") returns 6.
Read One Line at a Time
read() is useful when the complete file is small. A program can instead
iterate over the open file:
The loop receives one string at a time. Its output is:
1 'north,18.5\n'
2 'east,not recorded\n'
3 'west,24.0\n'
4 'café,21.5\n'
5 'north,19.0\n'
6 'west,23.5\n'
Line iteration avoids first building one string for the entire file. It also keeps the source line number beside each row. That number will matter when the program reports the invalid reading later.
Both forms decode text in the same way. The choice concerns how much text the program holds at once and whether its work is naturally organized by line.
| Reading form | Value produced | Useful when |
|---|---|---|
file.read() | one string containing the complete file | the complete text is small and needed together |
for line in file | one string for each successive line | work proceeds row by row or the file may be large |
Q2. Choose a reading form
A file may contain many measurement rows. The program validates and reports each row independently. Which reading form best matches that work?
Select one choice, then check.
HintMatch the unit of input to the unit of work
The program performs one validation decision for one row.
SolutionIterate over the file
A file loop supplies one line string at a time. The program can validate that row and retain its line number before reading the next one.
Reading Does Not Convert Fields
Even the row "north,18.5\n" is one string. The characters 1, 8, ., and
5 have not become the floating-point number 18.5. Reading and numerical
conversion are separate boundaries:
file row Python value after text reading
north,18.5 "north,18.5\n"
This separation is useful. The program first preserves what arrived, including the line ending and source line number. It can then split the stated fields, convert the reading text, and report a precise failure if conversion is not possible.
The runnable exercise below prepares the chapter's small file as supplied
browser data. The first with block is setup for this exercise; writing output
is taught later in the chapter. Complete only the read mode so the second block
prints every line with its newline visible.
Q3. Read the supplied file line by line
Replace "?" with the correct read mode. Run the program and inspect the six
strings returned by the file loop.
Editable Python
Ready to run.
HintUse the mode that does not change the file
The program needs to read existing text, so use the one-letter mode for reading.
SolutionOpen the file with read mode
mode = "r"
The loop then receives the six strings already stored in the supplied file.
Text mode gives the program decoded strings, either as one complete value or
one line at a time. A with statement closes the file when the reading block
ends, while explicit UTF-8 keeps the decoding rule visible. The next lesson
looks beneath that text boundary at the stored bytes.