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

W

walterbyrd

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
 
G

Grant Edwards

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.
 
J

James Stroud

Grant said:
No it isn't.





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




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
 
S

Steven Bethard

walterbyrd said:
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.

That's just not true. Did you try that code?
.... xxxxxxxxxx
.... xxxxxxxxxxx
.... xxxxxxx
....
.... xxxxxxxxxxxxxx
.... xxxxxxxxxx
.... x
.... ''').... print s,
.... s = f.readline()
........ print s,
.... s = f.readline()
....
xxxxxxxxxx
xxxxxxxxxxx
xxxxxxx

xxxxxxxxxxxxxx
xxxxxxxxxx
x

The file.readline() method returns '\n' for empty lines and '' for
end-of-file.

STeVe
 
D

Dan Bishop

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

Use a "for s in f" loop instead.
 
C

casevh

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
 
A

Asun Friere

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

The difference is simply that an empty line contains a '\n' while EOF
does not. If you strip() your line before testing you will have
trouble. But the minimal cases you post (properly indented and with
the missing ':' in place), should work (they just won't produce any
output). Repairing the first , I'm using dots (aka stops, periods) for
spaces here to stop the code getting munged :

line = fobj.readline()
while line :
.....print line.strip()
.....line = fobj.realine()

This does work look at this output (and note the empty lines):
line with stuff
line with more stuff

line after the empty line and before another

last line

In python it is more ideomatic to write this general kind of loop with
a break statement, thus:

while True :
.....line = fobj.readline()
.....if not line : break
.....print line.strip()

However since file has for a long time been an iterable the easiest
and most readible way to write it is this:

for line in fobj :
.....print line.strip()

Asun
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top