The short answer to your question is that each of these three methods of reading bits of a file have different use cases. As noted above, f.read()
reads the file as an individual string, and so allows relatively easy file-wide manipulations, such as a file-wide regex search or substitution.
f.readline()
reads a single line of the file, allowing the user to parse a single line without necessarily reading the entire file. Using f.readline()
also allows easier application of logic in reading the file than a complete line by line iteration, such as when a file changes format partway through.
Using the syntax for line in f:
allows the user to iterate over the file line by line as noted in the question.
(As noted in the other answer, this documentation is a very good read):
https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects
Note:
It was previously claimed that f.readline()
could be used to skip a line during a for loop iteration. However, this doesn't work in Python 2.7, and is perhaps a questionable practice, so this claim has been removed.
timeit
measurement forread
, or forreadlines
? I'd expect theread
loop to take longer because it returns a single string, so iterating over it would go character-by-character. If your file has on average 100 characters per line, then the code in thefor line in l.read()
loop will execute a hundred times as many times as the code in thefor line in l:
loop. – Abaddon