Seek the one billionth line in a file containing 3 billion lines.

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Aug 8 12:10:57 CEST 2007


Jay Loden a écrit :
(snip)
> If we just want to iterate through the file one line at a time, why not just:
> 
> count = 0
> handle = open('hugelogfile.txt')
> for line in handle.xreadlines():
>     count = count + 1
>     if count == '1000000000':
>         #do something

for count, line in enumerate(handle):
     if count == '1000000000':
         #do something


NB : files now (well... since 2.3) handle iteration directly
http://www.python.org/doc/2.3/whatsnew/node17.html



More information about the Python-list mailing list