efficient 'tail' implementation

Jerry Sievers jerry at jerrysievers.com
Thu Dec 8 08:35:50 EST 2005


s99999999s2003 at yahoo.com writes:

> hi
> 
> I have a file which is very large eg over 200Mb , and i am going to
> use python to code a "tail" command to get the last few lines of the
> file. What is a good algorithm for this type of task in python for
> very big files?  Initially, i thought of reading everything into an
> array from the file and just get the last few elements (lines) but
> since it's a very big file, don't think is efficient.  thanks


First of all, what makes you think that tail on your system isn't
already optinized for this?  Devil's advocate here... I have no clue
really.

Anyway, if you must roll your own;

Determine some reasonable max line size,, multiply this by a few
larger than the numbers of lines that you want to tail, seek to EOF
and then back to the position in the file where this chunk would
start, get and split that chunk into lines and now output the last 3
or however many you need.

If the read fails to hold enough lines, seek back a bit further and do
same but you'll have to be prepared to concat the second and Nth last
chunks together.

Have fun


-- 
-------------------------------------------------------------------------------
Jerry Sievers   305 854-3001 (home)     WWW ECommerce Consultant
                305 321-1144 (mobile	http://www.JerrySievers.com/



More information about the Python-list mailing list