[Python-ideas] shutil.tail(file, lines)

Antoine Pitrou solipsis at pitrou.net
Tue Nov 8 23:14:07 CET 2011


On Tue, 8 Nov 2011 19:20:45 +0100
Giampaolo Rodolà <g.rodola at gmail.com>
wrote:
> This is something I need to do every once in a while and I think it
> would be a good addition for shutil module.
> Here:
> http://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file-with-python-similar-to-tail
> ...is a nice implementation which appears to be a good compromise in
> terms of speed and memory consumption (it reads the file in chunks, no
> more than 1024 bytes per-read).

Well, is it supposed to be a text file or a binary file?
With a binary file the above approach is ok (you can use an adaptative
average line length if you want to be smarter).
With a text file and a generic encoding (possible weird or nasty) you
have no other solution than reading the file from the start.

Regards

Antoine.





More information about the Python-ideas mailing list