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

casevh at gmail.com casevh at gmail.com
Thu May 17 00:43:46 EDT 2007


On May 16, 2:47 pm, walterbyrd <walterb... at iname.com> wrote:
> Python's lack of an EOF character is giving me a hard time.
>
> I've tried:
>
> -----
> s = f.readline()
> while s:
> .
> .
> s = f.readline()
> --------
>
> and
>
> -------
> s = f.readline()
> while s != ''
> .
> .
> s = f.readline()
> -------
>
> In both cases, the loop ends as soon it encounters an empty line in
> the file, i.e.
>
> xxxxxxxxxx
> xxxxxxxxxxx
> xxxxxxx
>                           < - - -  loop end here
> xxxxxxxxxxxxxx
> xxxxxxxxxx
> x
>                            < ---- loop should end here

Assuming f is initialized as in your example, try

---------
for s in f:
    print s
---------

casevh




More information about the Python-list mailing list