if -seek_offset > in_file_length:
    block_size -= -seek_offset - in_file_length
if block_size == 0:
    break
seek_offset = -in_file_length
in_file.seek(seek_offset, os.SEEK_END)
buf = in_file.read(block_size)
if ignore_ending_newline and seek_offset == -block_size and buf[-1] == '\n':
    buf = buf[:-1]
pos = buf.rfind('\n')
if pos != -1:
    return buf[pos + 1:] + suffix
suffix = buf + suffix
return suffix
Efficiently finding the last line in a text file