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

James Stroud jstroud at mbi.ucla.edu
Wed May 16 18:08:07 EDT 2007


Grant Edwards wrote:
> 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.
> 

I'm guessing it was runnable when he pasted it into google groups.

James



More information about the Python-list mailing list