eof

MonkeeSage MonkeeSage at gmail.com
Sat Nov 24 06:03:33 CET 2007


On Nov 23, 10:43 pm, "hda... at gmail.com" <hda... at gmail.com> wrote:

>  This is not the same as ISO C. f.tell could be equal to
> File.size(f.path) and eof could be false. An extra read() is required.

My bad. As you might have surmised, I'm not a genius when it comes to
C. I thought that the eof flag was set when the pointer into the
stream was the same as the length of the stream, but I guess it makes
since that as an error flag, it would only be set after an attempted
read past the end of the stream (in which case, I guess you'd get a
NULL from the read, equivalent to python's empty string?).

Ps. braver, if you really want a ruby-like eof method on file objects
in python, how about overriding the open() alias with a file subclass
including eof?

import os

class open(file):
  def __init__(self, name):
    self.size = os.stat(name).st_size
    file.__init__(self, name)
  def eof(self):
    return self.tell() == self.size

f = open('tmp.py')
print f.eof() # False
f.read()
print f.eof() # True

Regards,
Jordan



More information about the Python-list mailing list