Поэтому мне нужен метод Tail(), который может читать n строк из дно и поддерживать смещение. Вот что у меня получилось:
Код: Выделить всё
def tail(f, n, offset=0):
"""Reads a n lines from f with an offset of offset lines."""
avg_line_length = 74
to_read = n + offset
while 1:
try:
f.seek(-(avg_line_length * to_read), 2)
except IOError:
# woops. apparently file is smaller than what we want
# to step back, go to the beginning instead
f.seek(0)
pos = f.tell()
lines = f.read().splitlines()
if len(lines) >= to_read or pos == 0:
return lines[-to_read:offset and -offset or None]
avg_line_length *= 1.3
Подробнее здесь: https://stackoverflow.com/questions/136 ... ar-to-tail