472,141 Members | 970 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,141 software developers and data experts.

skip last line in loops

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

Dec 15 '06 #1
15 8859
ei***********@yahoo.com wrote:
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
Dec 15 '06 #2
James Stroud wrote:
ei***********@yahoo.com wrote:
>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
Shoule be

aline = xlines.next()
Dec 15 '06 #3
Try:
afile = open(filename)
lines = afile.readlines()[:-1] # assigns all except the last element to
a list "lines"
for line in lines:
print line

Dec 15 '06 #4
ei***********@yahoo.com wrote:
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>

Dec 15 '06 #5

Fredrik Lundh wrote:
ei***********@yahoo.com wrote:
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...?

Dec 15 '06 #6
ei***********@yahoo.com wrote:
>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.

--
Roberto Bonvallet
Dec 15 '06 #7
ei***********@yahoo.com wrote:
Fredrik Lundh wrote:
>ei***********@yahoo.com wrote:
>>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
Dec 15 '06 #8
James Stroud wrote:
See the documentation for xreadlines.
why?

</F>

Dec 15 '06 #9
ei***********@yahoo.com writes:
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
Dec 15 '06 #10
On 14 Dec 2006 22:47:23 -0800, ei***********@yahoo.com wrote:
>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
Dec 15 '06 #11
ei***********@yahoo.com wrote:
>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>

Dec 15 '06 #12
Fredrik Lundh wrote:
James Stroud wrote:
>See the documentation for xreadlines.

why?

</F>
5.16 xreadlines -- Efficient iteration over a file
Dec 15 '06 #13
James Stroud wrote:
Fredrik Lundh wrote:
>James Stroud wrote:
>>See the documentation for xreadlines.

why?

</F>
5.16 xreadlines -- Efficient iteration over a file
http://www.python.org/dev/peps/pep-0004/

The cheat sheet for the effbot quiz :-)

Peter
Dec 15 '06 #14
lines = open('blah').readlines()
for i in range(0, len(lines)-1) :
print lines[i]

ei***********@yahoo.com wrote:
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
Dec 17 '06 #15
See the documentation for xreadlines.
>
James
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.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 18 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ramon Felciano | last post: by
reply views Thread by TK | last post: by
3 posts views Thread by puzzlecracker | last post: by
24 posts views Thread by Robin Cole | last post: by
4 posts views Thread by Jacob Rael | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.