tail
Marco Sulla
Marco.Sulla.Python at gmail.com
Sun May 1 12:55:41 EDT 2022
Something like this is OK?
import os
def tail(f):
chunk_size = 100
size = os.stat(f.fileno()).st_size
positions = iter(range(size, -1, -chunk_size))
next(positions)
chunk_line_pos = -1
pos = 0
for pos in positions:
f.seek(pos)
chars = f.read(chunk_size)
chunk_line_pos = chars.rfind(b"\n")
if chunk_line_pos != -1:
break
if chunk_line_pos == -1:
nbytes = pos
pos = 0
f.seek(pos)
chars = f.read(nbytes)
chunk_line_pos = chars.rfind(b"\n")
if chunk_line_pos == -1:
line_pos = pos
else:
line_pos = pos + chunk_line_pos + 1
f.seek(line_pos)
return f.readline()
This is simply for one line and for utf8.
More information about the Python-list
mailing list