[Tutor] is there an explicit eof to test in Py 3?

eryksun eryksun at gmail.com
Mon Apr 22 07:25:46 CEST 2013


On Sun, Apr 21, 2013 at 9:35 PM, Jim Mooney <cybervigilante at gmail.com> wrote:
> I'm reading a book that suggests finding EOF when the readLine == ""
>
>  But wouldn't that end erroneously on blank lines, that really contain
> '\n', in which case more lines might follow? What 'empties' are
> considered equal in Python? I'm coming from javascript which has a
> cluster of rules for that.

The newline (os.linesep) isn't stripped, though depending on the
"newline" option for open() it may be translated.

You shouldn't be testing equality. You know that reading from a file
returns either bytes or a string, so just rely on the boolean value.
Any type that implements __len_ (collections.abc.Sized) has an
implicit boolean value:

http://docs.python.org/3/reference/datamodel.html#object.__bool__

For example:

    while True:
        line = file.readline()
        if not line:
            break
        process(line)


More information about the Tutor mailing list