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?
source
share