Section 11.5 Alternative File Reading Methods
Again, recall the contents of the ccdata.txt file.
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 11.5.1 summarizes these methods and the following session 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
readlines the marker is moved to the end of the file.
>>> infile = open("ccdata.txt", "r") >>> aline = infile.readline() >>> aline '1850\-0.37\2.24E-7\n' >>> >>> infile = open("ccdata.txt", "r") >>> linelist = infile.readlines() >>> print(len(linelist)) 18 >>> print(linelist[0:4]) ['1850\-0.37\2.24E-7\n', '1860\-0.34\3.94E-7\n', '1870\-0.28\6.6E-7\n', '1880\-0.24\1.1\n'] >>> >>> infile = open("ccdata.txt", "r") >>> filestring = infile.read() >>> print(len(filestring)) 1282 >>> print(filestring[:256]) 1850 -0.37 2.24E-7 1860 -0.34 3.94E-7 1870 -0.28 6.6E-7 1880 -0.24 >>>
||Add astring to the end of the file. filevar must refer to a file that has been opened for writing.|
||Reads and returns a string of
||Returns the next line of the file with all text up to and including the newline character. If n is provided as a parameter than only n characters will be returned if the line is longer than
||Returns a list of strings, each representing a single line of the file. If n is not provided then all lines of the file are returned. If n is provided then n characters are read but 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 we'll show you here.
There are several important things to notice in this code:
On line 2 we have the statement
line = infile.readline(). We call this initial read the priming read. It is very important because the while condition needs to have a value for the
readline method will return the empty string if there is no more data in the file. An empty string is an empty sequence of characters. When Python is looking for a Boolean condition, as in
while line:, it treats an empty sequence type as
False, and a non-empty sequence as
True. 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
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.