eof

jjnoakes at gmail.com jjnoakes at gmail.com
Sat Dec 8 13:02:42 EST 2007


On Nov 24, 12:03 am, MonkeeSage <MonkeeS... at gmail.com> wrote:
>
> 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

This is a bad idea because self.size is not guaranteed to be accurate.
For files in /proc, /sys, and for fifo's, unix domain sockets, etc,
you will get incorrect results.

Always write your algorithm to simply read until there is no more
data. Once that happens, you are at EOF.

EOF is a property of a file only after reading didn't return any data.
If you use EOF in the middle of reading a file, it will never be
accurate (or it will be slow).

-JJ



More information about the Python-list mailing list