The Python Oracle

Difference between iterating over a file-like and calling readline

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Droplet of life

--

Chapters
00:00 Difference Between Iterating Over A File-Like And Calling Readline
00:32 Accepted Answer Score 5
01:18 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.