skip last line in loops
Fredrik Lundh
fredrik at pythonware.com
Fri Dec 15 11:25:26 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>
>
> 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>
More information about the Python-list
mailing list