Iterating through files

Iteration is used frequently with files; iteration can be used to read the information in the file and process it in an orderly manner. It also limits the amount of memory taken up when a file is read, which not only reduces system resource use but can also improve performance. The following screenshot demonstrates this:

Simple file iteration

Basically, the code in line 80 opens a temporary file in memory and reads each line from afile.txt, printing each one to the screen, until the end-of-file marker is reached. Unlike previous examples, using a for loop won't print the EOF marker because, once that marker is reached, file processing is aborted.

Line 81 shows the same thing, except that the print() function is provided with an argument for how to end each line. In this case, we just want to have an empty value to cause the lines to be printed without an intervening space.

When iterating through files, it is important to note that the readlines() method requires the file to be placed in memory before it can be processed; for large files, this can result in a performance hit or out-of-memory errors. Therefore, it may be better to look at one of the other file methods to process each line individually or come up with another solution.