reading from sys.stdin
Matimus
mccredie at gmail.com
Fri Apr 13 00:32:35 EDT 2007
On Apr 12, 8:20 am, Maric Michaud <m... at aristote.info> wrote:
> Le jeudi 12 avril 2007 16:25, Matimus a écrit :
>
> > # Then you check to see if your file is interactive
> > if f.isatty():
> > # This is unbuffered, and will iterate the same as f
> > f = iter(raw_input(),"")
>
> This should be f = iter(raw_input,"") and this will end in a EOFError and stop
> on blank line.
> So you need a wrapper like :
>
> >>> def stdin_iterator() :
>
> ... while(True) :
> ... try : yield raw_input()
> ... except EOFError : return
> ...
>
> >>> f = stdin_iterator()
>
> Do you really need to iterate on the file this way instead of using the
> straightforward readline method ?
>
> >>> import sys
> >>> l=sys.stdin.readline()
> >>> while(l) :
>
> ... print l,
> ... l=sys.stdin.readline()
>
> --
> _____________
>
> Maric Michaud
> _____________
>
> Aristote -www.aristote.info
> 3 place des tapis
> 69004 Lyon
> Tel: +33 4 26 88 00 97
> Mobile: +33 6 32 77 00 21
You are correct, I looked back to a program I wrote a while ago where
I ran into this issue. Here is the actual solution I used:
if f.isatty():
f = iter(f.readline,"")
More information about the Python-list
mailing list