Reading just a few lines from a text file

John Machin sjmachin at lexicon.net
Tue Aug 23 19:11:01 EDT 2005


tkpmep at hotmail.com wrote:
> I have a text file with many hundreds of lines of data. The data of
> interest to me, however, resides at the bottom of the file, in the last
> 20 lines. Right now, I read the entire file and discard the stuff I
> don't need. I'd like to speed up my program by reading only the last 20
> lines. How do I do this?
> 
> Thomas Philips
> 

What discernible speed increase are you talking about? How long does it 
take to read the "many hundreds" of lines?

For "many hundreds", IMHO it's not worth the bother, the complexity, the 
documentation, ...

Just do this, it's about as fast as you'll get in pure python for a 
smallish file:

def last_n_lines(filename, n):
    return open(filename, 'r').readlines()[-n:]

For many hundreds of thousands of lines, one approach might be to open 
the file in binary mode, seek to the end of the file, then loop reading 
chunks backwards and unpacking the chunks until you've found 21 line 
terminators. Or perhaps 20 line separators :-)



More information about the Python-list mailing list