reading from sys.stdin
Steve Holden
steve at holdenweb.com
Sun Apr 15 12:59:24 EDT 2007
7stud wrote:
> On Apr 14, 7:43 am, Steve Holden <s... at holdenweb.com> wrote:
>> 7stud wrote:
>>> On Apr 13, 6:20 am, Michael Hoffman <cam.ac... at mh391.invalid> wrote:
>> [...]
>>
>>> 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?
>> No. The file content is usually buffered, but the buffering doesn't
>> necessarily include the whole content of the file.
>>
>> If you are iterating over the file the correct way to access the next
>> line is to call the file's .next() method, as I indicated before.
>>
>> If you are reading lines the appropriate way is to use readline().
>>
>> And, as you have already seen an error message telling you, mixing the
>> two types is unlikely to give you usable results.
>>
> Does iterating over stdin work the same way? If one were to type in
> enough lines to fill the buffer would iteration begin before entering
> EOF with Ctrl+D?
>
Why don't you try it and tell me? That's what the interactive
interpreter is for.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
More information about the Python-list
mailing list