Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Mimick tac with python. Date: Sat, 30 Jan 2016 09:21:18 +0100 Organization: None Lines: 40 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de 2Z1ck0+ylvXBUDeoDkV2qQF+HcxGWhuuoH+PjN19m07A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'memory.': 0.05; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'filename': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sucking': 0.09; 'jan': 0.11; 'output': 0.13; 'size,': 0.13; 'def': 0.13; 'awk': 0.16; 'b""': 0.16; 'curious.': 0.16; 'f.tell()': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:python.': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'input': 0.18; '>>>': 0.20; 'import': 0.24; 'implemented': 0.24; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'fri,': 0.27; 'yield': 0.27; 'perl': 0.29; "i'm": 0.30; 'print': 0.30; 'file': 0.34; 'that,': 0.34; 'could': 0.35; 'sometimes': 0.35; 'but': 0.36; 'lines': 0.36; 'possible': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'christian': 0.38; 'skip:s 40': 0.38; 'end': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'received:de': 0.40; 'more': 0.63; 'gollwitzer': 0.84; 'remembering': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8b68.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102286 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.