file object and eof

KraftDiner bobrien18 at yahoo.com
Tue Aug 15 15:06:26 EDT 2006


Grant Edwards wrote:
> On 2006-08-15, KraftDiner <bobrien18 at yahoo.com> wrote:
>
> > I open a file in python by
> > f = open('filename', mode='rb')
> >
> > how can I tell if I am at the end of file?
>
> I don't believe you can unless you try to read from the file.
>
> > f.eof() isn't implmented.
> >
> > How can I implement its functionallity?
>
> You don't, generally.
>
> There's probably a better, "more Pythonic" way to accomplish
> your goal, but you're going to have to tell us what it is.
>

how about?!:

def eof(fileobj):
   curloc = fileobj.tell()
   ofloc = fileobj.seek(0,2)
   fileobj.seek(curloc, 0)
   if ofloc >= curloc:
      return True
   return False




More information about the Python-list mailing list