how to read the last line of a huge file???

kost BebiX k.bx at ya.ru
Thu Jan 27 09:25:03 EST 2011


27.01.2011, 15:55, "Roy Smith" <roy at panix.com>:
> In article <mailman.1353.1296119065.6505.python-list at python.org>;,
>  Alice Bevan–McGregor <alice at gothcandy.com>; wrote:
>
>>  On 2011-01-26 02:59:26 -0800, Xavier Heruacles said:
>>>  I have do some log processing which is usually huge. The length of each
>>>  line is variable. How can I get the last line?? Don't tell me to use
>>>  readlines or something like linecache...
>>  This is not optimum or efficient, but it works!  If you want to see
>>  what's going on, use 4 instead of 4096 and 8 instead of 8192 and add a
>>  print statement to the bottom of the while loop.  :)
>>
>>  import os
>>
>>  with open('biglogfile.log', 'r') as fh:
>>      fh.seek(-4096, os.SEEK_END)
>>      buffer = fh.read(4096)
>>
>>      # We are expecting a trailing newline.
>>      while "\n" not in buffer[:-1]:
>>          fh.seek(-8192, os.SEEK_CUR)
>>          buffer = fh.read(4096) + buffer
>>
>>      # Eliminate empty lines, they aren't useful.
>>      lines = [line for line in buffer.split('\n') if line]
>>      print lines[-1]
>>
>>          — Alice.  :)
>
> Back in the old days, if you wanted to read a file backwards, you just
> used a tape drive that had read-reverse capability :-)
>
> --
> http://mail.python.org/mailman/listinfo/python-list

Yeah. And if you wanted to zoom image -- you just had to go couple steps closer to it)

-- 
jabber: k.bx at ya.ru



More information about the Python-list mailing list