skip last line in loops

E

eight02645999

hi,
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.thanks
 
J

James Stroud

hi,
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.thanks

afile = open(filename)

xlines = afile.xreadlines()

aline = xlines.next
for nextline in xlines:
print aline
aline = nextline

James
 
J

James Stroud

James said:
afile = open(filename)

xlines = afile.xreadlines()

aline = xlines.next
for nextline in xlines:
print aline
aline = nextline

James

Shoule be

aline = xlines.next()
 
T

tac-tics

Try:
afile = open(filename)
lines = afile.readlines()[:-1] # assigns all except the last element to
a list "lines"
for line in lines:
print line
 
F

Fredrik Lundh

how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.

do it lazily:

last_line = None
for line in open("file):
if last_line:
print last_line
last_line = line

or just gobble up the entire file, and slice off the last item:

for line in list(open("file"))[:-1]:
print line

</F>
 
E

eight02645999

Fredrik said:
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.

do it lazily:

last_line = None
for line in open("file):
if last_line:
print last_line
last_line = line

or just gobble up the entire file, and slice off the last item:

for line in list(open("file"))[:-1]:
print line

</F>

hi
would it be a problem with these methods if the file is like 20Gb in
size...?
 
R

Roberto Bonvallet

do it lazily:

last_line = None
for line in open("file):
if last_line:
print last_line
last_line = line

or just gobble up the entire file, and slice off the last item:

for line in list(open("file"))[:-1]:
print line

</F>

hi
would it be a problem with these methods if the file is like 20Gb in
size...?

The second one would be a problem, since it creates a list containing all
the lines of the file. Use the lazy approach.
 
J

James Stroud

Fredrik said:
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.
do it lazily:

last_line = None
for line in open("file):
if last_line:
print last_line
last_line = line

or just gobble up the entire file, and slice off the last item:

for line in list(open("file"))[:-1]:
print line

</F>

hi
would it be a problem with these methods if the file is like 20Gb in
size...?

See the documentation for xreadlines.

James
 
P

Paul Rubin

for line in open("file):
print line.

I want to skip printing last line of the file.thanks

def all_but_last(it): # yield all but last item of an iterator
a = it.next()
for b in it:
yield a
a = b

for line in all_but_last(open("file")):
print line
 
D

Daniel Klein

hi,
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.thanks

while True:
line1 = myfile.readline()
if not line1: break
line2 = myfile.readline()
if line2:
print line1
else:
break


Dan
 
F

Fredrik Lundh

do it lazily:

last_line = None
for line in open("file):
if last_line:
print last_line
last_line = line

or just gobble up the entire file, and slice off the last item:

for line in list(open("file"))[:-1]:
print line

</F>
>
would it be a problem with these methods if the file is like 20Gb in
size...?

not with the lazy version, of course. the "gobble up" version will
load the entire file into memory.

but cutting off a single line from a 20 gigabyte file by looping over
it sounds like a bit contrived, really. if you're really doing this
(why?), maybe you should just truncate the file in place instead.

</F>
 
T

tobiah

See the documentation for xreadlines.

Hmm...

This method returns the same thing as iter(f). New in version 2.1.
Deprecated since release 2.3. Use "for line in file" instead.
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top