Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between iterating over a file-like and calling readline

I always thought iterating over a file-like in Python would be equivalent to calling its readline method in a loop, but today I found a situation where that is not true. Specifically, I have a Popen'd process p where

list(itertools.takewhile(lambda x: x != "\n",
                         p.stdout))

hangs (presumably because p waits for input; both stdin and stdout are pipes to my Python process), while the following works:

list(itertools.takewhile(lambda x: x != "\n",
                         iter(p.stdout.readline, "")))

Can someone explain the difference?

like image 583
Fred Foo Avatar asked Apr 04 '12 13:04

Fred Foo


People also ask

What is difference between readline and Readlines?

Python readline() method will return a line from the file when called. readlines() method will return all the lines in a file in the format of a list where each element is a line in the file.

What is the difference between read () and readline () function in Python?

The read() will read the whole file at once and then print out the first characters that take up as many bytes as you specify in the parenthesis versus the readline() that will read and print out only the first characters that take up as many bytes as you specify in the parenthesis.

What does file readline () do?

readline() function reads a line of the file and return it in the form of the string. It takes a parameter n, which specifies the maximum number of bytes that will be read.

What is the difference between the read () readline () methods?

The only difference between the Read() and ReadLine() is that Console. Read is used to read only single character from the standard output device, while Console. ReadLine is used to read a line or string from the standard output device. Program 1: Example of Console.


1 Answers

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.

like image 83
Thomas Wouters Avatar answered Oct 07 '22 09:10

Thomas Wouters