Difference between file-like and calling read iteration

I always thought that file-like iteration in Python is equivalent to calling a method readlinein a loop, but today I found a situation where this is not the case. In particular, I have a process Popen' d p, where

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

freezes (presumably because it pexpects input, both stdinand stdoutare pipes for my Python process), and the following works:

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

Can someone explain the difference?

+5
source share
1 answer

readline. ( 8 ), . , readline , , . , , . , , , , . readline .

, iter(f.readline, '') , .

+4

All Articles