reading the last line of a file

Fredrik Lundh fredrik at pythonware.com
Thu Sep 8 07:26:46 EDT 2005


Fredrik Lundh wrote:

> zcat|tail is a LOT faster.

and here's the "right way" to use that:

    from subprocess import Popen, PIPE
    p1 = Popen(["zcat", filename], stdout=PIPE)
    p2 = Popen(["tail", "-1"], stdin=p1.stdout, stdout=PIPE)
    last_line = p2.communicate()[0]

(on my small sample, this is roughly 15 times faster than the empty for loop)

</F> 






More information about the Python-list mailing list