named pipe input

Donn Cave donn at
Fri Sep 2 01:24:55 CEST 2005

In article <3gIRe.6091$O6.328317 at>,
 "max(01)*" <max2 at> wrote:

> i have some problems understanding following behaviour.
> consider this:

> $ cat
> #!/usr/bin/perl
> open MIAPIPE, "una_pipe";
> while ($riga = <MIAPIPE>)

> $ cat
> #!/usr/bin/python
> import sys
> MIAPIPE = open("una_pipe", "r")
> for riga in MIAPIPE:

> BUT if i try to do the same with the python code, something different 
> happens: i have to type ALL the lines on console #2 and complete the cat 
> command (ctrl-d) before seeing the lines echoed on console #1.

Seems to me something like this came up here
not long ago.  It turns out that

  for line in file:

doesn't do the same thing as Perl's

   while ($line = <file>)

If you use file.readline() instead (in a loop,
of course, I think you'll get the data one line
at a time, but "in file" apparently reads the
whole file first.  That's what I vaguely remember,
I don't use it myself.

   Donn Cave, donn at

More information about the Python-list mailing list