skip last line in loops

Roberto Bonvallet Roberto.Bonvallet at cern.ch
Fri Dec 15 06:51:40 EST 2006


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



More information about the Python-list mailing list