Silly question re: 'for i in sys.stdin'?

David Trudgett wpower at zeta.org.au.nospamplease
Sun Apr 3 22:46:54 EDT 2005


I'm not a Python expert by any means, but you're describing the
classic symptoms of buffering. There is a '-u' command line switch for
python to turn off buffering but that does not affect file iterators. 
See http://www.hmug.org/man/1/python.html for instance.

Tom Eastman <tom at cs.otago.ac.nz> writes:

> I'm not new to Python, but I didn't realise that sys.stdin could be called
> as an iterator, very cool!
>
> However, when I use the following idiom:
>
>    for line in sys.stdin:
>        doSomethingWith(line)

Guess what the suggested work-around on the man page was? Use
sys.stdin.readline() in a "while 1:" loop, as you have below:

>
>    while True:
>       line = sys.stdin.readline()
>       if line == '': break
>       doSomethingWith(line)

David

-- 

David Trudgett
http://www.zeta.org.au/~wpower/

Reality is 20% real and 80% made up stuff in your head.
But I'm not sure about the 20%.



More information about the Python-list mailing list