skip last line in loops

Paul Rubin http
Fri Dec 15 08:41:35 EST 2006


eight02645999 at 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



More information about the Python-list mailing list