def reverse_readline(fh, start_byte=0, buf_size=8192):
 segment = None
 offset = 0
 if start_byte:
  fh.seek(start_byte)
 else:
  fh.seek(0, os.SEEK_END)
 total_size = remaining_size = fh.tell()
 while (remaining_size > 0):
  offset = min(total_size, (offset + buf_size))
  fh.seek((- offset), os.SEEK_END)
  buf = fh.read(min(remaining_size, buf_size))
  remaining_size -= buf_size
  lines = buf.decode(sys.getfilesystemencoding()).split(u'\n')
  if (segment is not None):
   if (buf[(-1)] is not u'\n'):
    lines[(-1)] += segment
   else:
    (yield segment)
  segment = lines[0]
  for index in range((len(lines) - 1), 0, (-1)):
   if len(lines[index]):
    (yield lines[index])
 (yield segment)

def reverse_readline ( fh , start_byte = 0 , buf_size = 8192 ) : segment = None offset = 0 if start_byte : fh . seek ( start_byte ) else : fh . seek ( 0 , os . SEEK_END ) total_size = remaining_size = fh . tell ( ) while ( remaining_size > 0 ) : offset = min ( total_size , ( offset + buf_size ) ) fh . seek ( ( - offset ) , os . SEEK_END ) buf = fh . read ( min ( remaining_size , buf_size ) ) remaining_size -= buf_size lines = buf . decode ( sys . getfilesystemencoding ( ) ) . split ( u'\n' ) if ( segment is not None ) : if ( buf [ ( - 1 ) ] is not u'\n' ) : lines [ ( - 1 ) ] += segment else : ( yield segment ) segment = lines [ 0 ] for index in range ( ( len ( lines ) - 1 ) , 0 , ( - 1 ) ) : if len ( lines [ index ] ) : ( yield lines [ index ] ) ( yield segment )
a generator that returns the lines of a file in reverse order .