reading from sys.stdin

Maric Michaud maric at aristote.info
Thu Apr 12 17:20:13 CEST 2007


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



More information about the Python-list mailing list