By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,034 Members | 2,000 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,034 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

May 16 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
walterbyrd wrote:
Python's lack of an EOF character is giving me a hard time.

I've tried:
[ stuff ]

for s in f:
do_whatever_with_s(s)
James
May 16 '07 #2

P: n/a
On 2007-05-16, walterbyrd <wa********@iname.comwrote:
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?
May 16 '07 #3

P: n/a
Grant Edwards wrote:
On 2007-05-16, walterbyrd <wa********@iname.comwrote:

>>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
May 16 '07 #4

P: n/a
walterbyrd 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.
That's just not true. Did you try that code?
>>open('temp.txt', 'w').write('''\
.... xxxxxxxxxx
.... xxxxxxxxxxx
.... xxxxxxx
....
.... xxxxxxxxxxxxxx
.... xxxxxxxxxx
.... x
.... ''')
>>while s:
.... print s,
.... s = f.readline()
....
>>f = open('temp.txt')
s = f.readline()
while s:
.... 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
May 17 '07 #5

P: n/a
On May 16, 4:47 pm, walterbyrd <walterb...@iname.comwrote:
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.

May 17 '07 #6

P: n/a
On May 16, 2:47 pm, walterbyrd <walterb...@iname.comwrote:
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

May 17 '07 #7

P: n/a
On May 17, 7:47 am, walterbyrd <walterb...@iname.comwrote:
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

May 17 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.