Mimick tac with python.

Peter Otten __peter__ at web.de
Sat Jan 30 03:21:18 EST 2016


Christian Gollwitzer wrote:

> Am 30.01.16 um 05:58 schrieb Random832:
>> On Fri, Jan 29, 2016, at 23:46, Hongyi Zhao wrote:
>>> awk '{a[NR]=$0} END {while (NR) print a[NR--]}' input_file
>>> perl -e 'print reverse<>' input_file
>>
>> Well, both of those read the whole file into memory - tac is sometimes
>> smarter than that, but that makes for a more complex program.
> 
> Now I'm curious. How is it possible to output the first line as last
> again if not by remembering it from the every beginning? How could tac
> be implemented other than sucking up everything into memory?

If the input file is seekable you can do blockwise reads:

import os
import sys


def tac(f, blocksize=1024):
    buf = b""
    f.seek(0, os.SEEK_END)
    size = f.tell()
    for start in reversed(range(0, size, blocksize)):
        f.seek(start)
        buf = f.read(blocksize) + buf
        lines = buf.splitlines(True)
        buf = lines.pop(0)
        yield from reversed(lines)
    yield buf


if __name__ == "__main__":
    for filename in sys.argv[1:]:
        with open(filename, "rb") as infile:
            sys.stdout.buffer.writelines(tac(infile))

This way you need to keep one block plus one line in memory.




More information about the Python-list mailing list