tail
Dan Stromberg
drsalists at gmail.com
Sat May 7 12:54:28 EDT 2022
I believe I'd do something like:
#!/usr/local/cpython-3.10/bin/python3
"""
Output the last 10 lines of a potentially-huge file.
O(n). But technically so is scanning backward from the EOF.
It'd be faster to use a dict, but this has the advantage of working for
huge num_lines.
"""
import dbm
import os
import sys
tempfile = f'/tmp/{os.path.basename(sys.argv[0])}.{os.getpid()}'
db = dbm.open(tempfile, 'n')
num_lines = 10
for cur_lineno, line in enumerate(sys.stdin):
db[str(cur_lineno)] = line.encode('utf-8')
max_lineno = cur_lineno
str_age_out_lineno = str(cur_lineno - num_lines - 1)
if str_age_out_lineno in db:
del db[str_age_out_lineno]
for lineno in range(max_lineno, max_lineno - num_lines, -1):
str_lineno = str(lineno)
if str_lineno not in db:
break
print(db[str(lineno)].decode('utf-8'), end='')
db.close()
os.unlink(tempfile)
On Sat, Apr 23, 2022 at 11:36 AM Marco Sulla <Marco.Sulla.Python at gmail.com>
wrote:
> What about introducing a method for text streams that reads the lines
> from the bottom? Java has also a ReversedLinesFileReader with Apache
> Commons IO.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list