Skip to main content

Section 10.5 Alternative File Reading Methods

Again, recall the contents of the daily_weather.txt file.
Data: daily_weather.txt
2022-06-01,12,29
2022-06-02,13,25
2022-06-03,14,24
2022-06-04,15,24
2022-06-05,17,27
2022-06-06,14,25
2022-06-07,13,26
2022-06-08,14,26
2022-06-09,13,33
2022-06-10,17,36
2022-06-11,18,33
2022-06-12,15,27
2022-06-13,13,24
2022-06-14,14,30
2022-06-15,14,28
2022-06-16,12,23
2022-06-17,13,22
2022-06-18,11,23
2022-06-19,11,29
2022-06-20,13,31
2022-06-21,16,39
2022-06-22,21,36
2022-06-23,18,32
2022-06-24,17,31
2022-06-25,14,28
2022-06-26,15,27
2022-06-27,15,28
2022-06-28,14,28
2022-06-29,13,23
2022-06-30,14,23
In addition to the for loop, Python provides three methods to read data from the input file. The readline method reads one line from the file and returns it as a string. The string returned by readline will contain the newline character at the end. This method returns the empty string when it reaches the end of the file. The readlines method returns the contents of the entire file as a list of strings, where each item in the list represents one line of the file. It is also possible to read the entire file into a single string with read. Table 10.5.1 summarizes these methods and the following program shows them in action.
Note that we need to reopen the file before each read so that we start from the beginning. Each file has a marker that denotes the current read position in the file. Any time one of the read methods is called the marker is moved to the character immediately following the last character returned. In the case of readline this moves the marker to the first character of the next line in the file. In the case of read or readlines, the marker is moved to the end of the file.
Table 10.5.1.
Method Name Use Explanation
write file_var.write(a_string)
Add a_string to the end of the file. file_var must refer to a file that has been opened for writing.
read(n) file_var.read()
Reads the entire file as a single string, or, if an integer n is provided as an argument, returns a string of n characters, or the entire file as a single string if n is not provided.
readline(n) file_var.readline()
Returns the next line of the file with all text up to and including the newline character. If an integer n is provided as an argument, then only n characters will be returned if the line is longer than n characters.
readlines(n) file_var.readlines()
Returns a list of strings, each representing a single line of the file. If an argument n is not provided, then all lines of the file are returned. If n is provided, then n is rounded up so that an entire line is returned.
Now let’s look at another method of reading our file using a while loop. This is important because many other programming languages do not support the for loop style for reading files, but they do support the pattern shown here.
There are several important things to notice in this code:
On line 2 we have the statement line = in_file.readline(). We call this initial read the priming read. It is very important because the while condition needs to have a value for the line variable.
The readline method returns the empty string if there is no more data in the file. An empty string is an empty sequence of characters, represented as "". Remember that a blank line in the file actually has a single character, the \n character (newline). So, the only way that a line of data from the file can be empty is if you are reading at the end of the file, and the while condition becomes False.
Finally, notice that the last line of the body of the while loop performs another readline. This statement will reassign the variable line to the next line of the file. It represents the change of state that is necessary for the iteration to function correctly. Without it, there would be an infinite loop processing the same line of data over and over.