Difference between iterating over a file-like and calling readline
--
Track title: CC O Beethoven - Piano Sonata No 3 in C
--
Chapters
00:00 Question
00:48 Accepted answer (Score 5)
01:50 Thank you
--
Full question
https://stackoverflow.com/questions/1001...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #io #iterator #subprocess #pipe
#avk47
ACCEPTED ANSWER
Score 5
The difference is purely in the implementation of iteration versus the readline method. File iteration reads in blocks (of 8 kilobytes, by default) and then splits up the buffer into lines as you consume them. The readline method, on the other hand, takes care never to read more than one line, and that means reading character by character. Reading in blocks is much more efficient, but it means you can't mix other operations on the file between reads. The expectation is that when you are iterating over the file, your intent is to read all lines sequentially and you will not be doing other operations on it. The readline method can't make that assumption.
As Sven Marnach hinted in his comment to your question, you can use iter(f.readline, '') to get an iterator that reads lines from the file without reading in blocks, at the cost of performance.