stdio EOF ?

Jacek Generowicz jacek.generowicz at cern.ch
Tue Aug 13 11:33:46 CEST 2002


Erik Max Francis <max at alcyone.com> writes:

> Jacek Generowicz wrote:
> 
> > Using "for line is stdin: ..." works if all the commands are in a file
> > and I do
> > 
> >   cat command_file | filter.py | etc
> > 
> > but in the "cat | filter.py" situation, it doesn't produce any output
> > until C-d is sent.
> > 
> > I guess I'm looking for a "while something(stdin)" or "while
> > stdin.something" construct ...
> 
> The standard idiom for this is:
> 
> 	while 1:
> 	   line = file.readline()
> 	   if not line:
> 	      break
> 	   ...
> 
> The .readline method of a file object returns a complete line (including
> the trailing newline), or an empty string in the case of EOF.

... unfortunately empty lines are meaningful in the relevant context
(separation of datasets); an empty line has a meaning different from
that of EOF (no more datasets after this one).

Cheers,



More information about the Python-list mailing list