During my work, I need to parse the result from some huge log files. The result is always save at the end of the file. So I need find a way to get the required last lines to accelerate the parse speed. Here is the function I have written to achieve this requirement in python.
- def last_lines(filename, lines=1):
 -     '''
 -     return the last lines from the specify files
 -     '''
 -     with open(filename, 'rb') as fh:
 -         fh.seek(0, os.SEEK_END)
 -         if fh.tell() == 0:
 -             return []
 -         fh.seek(-2, os.SEEK_END)
 -         #while not BOF
 -         while fh.tell() > 0:
 -             cur_char = fh.read(1).decode()
 -             if cur_char == '\n':
 -                 lines -= 1
 -             if lines > 0:
 -                 fh.seek(-2, os.SEEK_CUR)
 -             else:
 -                 break
 -         return [line.decode() for line in fh.readlines()]
 


