reading from sys.stdin

Diez B. Roggisch deets at nospam.web.de
Thu Apr 12 04:34:10 EDT 2007


7stud schrieb:
> I can't break out of the for loop in this example:
> 
> ------
> import sys
> 
> lst = []
> for line in sys.stdin:
>     lst.append(line)
>     break
> 
> print lst
> -----------

Works for me. But only after the stdin is closed with a C-d.

I presume this is an OS thing. The first lines aren't communicated to 
the process until either the file is closed - C-d - or the buffer the OS 
puts before the stream is filled. You can switch to unbuffered behviour 
somehow, google for it. Termios should be in your query.

Either way, it's not python behaving differently.

Diez



More information about the Python-list mailing list