feof status (was: Re: [Python-Dev] Rehabilitating fgets)

Fredrik Lundh fredrik@effbot.org
Sun, 7 Jan 2001 22:29:46 +0100

Guido van Rossum wrote:
> Bizarre (given what I know about zero-length read).  But in the above
> code, you can replace "if not fp.feof()" with "if line".  In other
> words, you just have to carry the state over within your program.

and if that's too hard, just hide the state in
a class:

class FileWrapper:

    def __init__(self, file):
        self.__file = file
        self.__line = None

    def __more(self):
        # try reading another line
        if not self.__line:
            self.__line = self.__file.readline()

    def eof(self):
        return not self.__line

    def readline(self):
        line = self.__line
        self.__line = None
        return line

file = open("myfile.txt")

file = FileWrapper(file)

while not file.eof():
    print repr(file.readline())