How to read lines from end of a file?

Alex Martelli Alex.Martelli at think3.com
Thu Dec 23 09:28:58 EST 1999


Alexander Williams wrote:

> On 22 Dec 1999 21:52:31 +0100, Stig Bjorlykke <stigb at tihlde.org> wrote:
> >open FILE, "/tmp/file";
> >foreach (reverse <FILE>) { ... }
> >
> >I am using it to get the latest entries in a log file.
> 
> Thought about:
> 
>     >>> data = open("filename").readlines()
>     >>> data.reverse()
>     >>> for lne in data:
>     >>>     ...
> 
> Admittedly this gets rather hairy for long log file analysis since it
> has to slurp up the whole thing into memory; alternately, you can try
> 
I am pretty sure that the given Perl idiom also has to slurp the
whole file into memory, plus, since Perl's 'reverse' returns a
reversed copy of the list rather than reversing in-place, that
should consume _twice_ as much memory as the in-place
reversing that your Python equivalent does...

(some of us recent-ex-Perl-hackers are some of the most
enthusiastic Python newbies, I guess:-).


Alex





More information about the Python-list mailing list