reading from sys.stdin
Michael Hoffman
cam.ac.uk at mh391.invalid
Fri Apr 13 08:20:33 EDT 2007
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
More information about the Python-list
mailing list