How do I tell the difference between the end of a text file, and an empty line in a text file?

Grant Edwards grante at visi.com
Wed May 16 17:58:31 EDT 2007


On 2007-05-16, walterbyrd <walterbyrd at iname.com> wrote:

> Python's lack of an EOF character is giving me a hard time.

No it isn't.

> s = f.readline()
> while s:
> .
> .
> s = f.readline()



> s = f.readline()
> while s != ''
> .
> .
> s = f.readline()


Neither one of your examples is legal Python.  Please post real
code.

> In both cases, the loop ends as soon it encounters an empty line in
> the file, i.e.

No, it doesn't.  Not if you've done something reasonable like
this:

f = open('testdata','r')
while True:
    s = f.readline()
    if not s: break
    print repr(s)
          
or this:

f = open('testdata','r')
s = f.readline()
while s:
    print repr(s)
    s = f.readline()

Please post real, runnable code.  You've done something wrong
and we've no way to guess what it was if you won't show us your
code.

-- 
Grant Edwards                   grante             Yow! Is something VIOLENT
                                  at               going to happen to a
                               visi.com            GARBAGE CAN?



More information about the Python-list mailing list