def _seek_to_n_lines_from_end(f, numlines=10):
    """
    Seek to `numlines` lines from the end of the file `f`.
    """
    buf = ''
    buf_pos = 0
    f.seek(0, 2)
    line_count = 0
    while line_count < numlines:
        newline_pos = buf.rfind('\n', 0, buf_pos)
        file_pos = f.tell()
        if newline_pos == -1:
            if file_pos == 0:
                break
            else:
                toread = min(1024, file_pos)
                f.seek(-toread, 1)
                buf = f.read(toread) + buf[:buf_pos]
                f.seek(-toread, 1)
                buf_pos = len(buf) - 1
        else:
            buf_pos = newline_pos
            line_count += 1
    if line_count == numlines:
        f.seek(buf_pos + 1, 1)
