eof

I V ivlenin at gmail.com
Wed Nov 21 21:08:41 EST 2007


On Wed, 21 Nov 2007 17:06:15 -0800, braver wrote:
> Why do I have to count sizes of lines read and compare it to some
> filesize or do other weird tricks just to see, in a way not changing my
> input stream, whether it's at the, well, EOF?

Because you can't, generally, tell whether or not a stream is at the end 
of the file without reading from it; the C standard library doesn't 
guarantee that the EOF status is set until _after_ you've tried to read 
past the end of the file. I don't know, but I suspect that may be why 
python doesn't support querying files for EOF status - the fact that EOF 
can be false when you've read all the data in the file is a source of 
confusion to lots of C programmers.

It looks like ruby internally buffers the stream itself, which is how 
come it can support this. According to the docs:

"Note that IO#eof? reads data to a input buffer."

http://www.ruby-doc.org/core/classes/IO.html#M002309

I'd imagine that's inefficient in a lot of cases; and I don't think it 
provides much benefit. What's the hardship in checking the return values 
of your IO calls? Or iterating over the file, which is a more elegant 
solution in many cases.



More information about the Python-list mailing list