Checkpoint 8.4.1.
Q-2: When using a for loop to read lines, how many lines are read at a time?
for
loop to read through and count each of the lines in a file:for
loop. Our for
loop simply counts the number of lines in the file and prints them out. The rough translation of the for
loop into English is, “for each line in the file represented by the file handle object, add one to the count
variable.”open
function does not read the entire file is that the file might be quite large with many gigabytes of data. The open
statement takes the same amount of time regardless of the size of the file. The for
loop actually causes the data to be read from the file.for
loop in this manner, Python takes care of splitting the data in the file into separate lines using the newline character. Python reads each line through the newline and includes the newline as the last character in the line
variable for each iteration of the for
loop.for
loop reads the data one line at a time, it can efficiently read and count the lines in very large files without running out of main memory to store the data. The above program can count the lines in any size file using very little memory since each line is read, counted, and then discarded.read
method on the file handle.inp
. We use string slicing to print out the first 20 characters of the string data stored in inp
.inp
. It is a good idea to store the output of read
as a variable because each call to read
exhausts the resource:>>> fhand = open('mbox-short.txt')
>>> print(len(fhand.read()))
94626
>>> print(len(fhand.read()))
0
open
function should only be used if the file data will fit comfortably in the main memory of your computer. If the file is too large to fit in main memory, you should write your program to read the file in chunks using a for
or while
loop.