reading from sys.stdin

7stud bbxx789_05ss at yahoo.com
Sat Apr 14 04:22:10 EDT 2007


On Apr 13, 6:20 am, Michael Hoffman <cam.ac... at mh391.invalid> wrote:
> 7stud wrote:
> > On Apr 13, 3:13 am, Michael Hoffman <cam.ac... at mh391.invalid> wrote:
> >> 7stud wrote:
> >>> I assume all input is buffered by default, so I'm not sure how it
> >>> explains things to say that input from sys.stdin is buffered.
> >> The difference with sys.stdin is that it has indeterminate length until
> >> you signal EOF. I believe you'd get the same problem reading from, say,
> >> a named pipe.
>
> > Couldn't you say the same thing about a file you are iterating over?
>
> Only if the file has indeterminate length. Regular files have a length.
>
> >>>> This should be f = iter(raw_input,"") and this will end in a EOFError
> >>>> and stop on blank line. So you need a wrapper
> >>> Why a wrapper?
> >> Because without a wrapper you'll get EOFError, while the file iterator
> >> would ordinarily give you StopIteration.
>
> > Did you run my example?  Did you get an error?  I don't get an error.
>
> Yes I did. I did get an error.
>
>  >>> lst = []
>  >>> f = iter(raw_input, "")
>  >>> for line in f:
> ...     lst.append(line)
> ...
> abc
> def
> <Ctrl-D>Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> EOFError
> --
> Michael Hoffman

But if you hit return on a blank line, there is no error.  In other
words, will stop on a blank line and not return EOFError.

Anyway, it seems everyone is saying that when you iterate over a file,
the whole file is first read into memory.  Therefore iterating over
sys.stdin is consistent: you have to type Ctrl+D to signal EOF before
the iteration can start.  Is that about right?




More information about the Python-list mailing list