efficient 'tail' implementation

Colin J. Williams cjw at sympatico.ca
Fri Dec 9 16:12:43 CET 2005


s99999999s2003 at yahoo.com wrote:
> 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
> 
If your file is seekable you could consider using file's seek function.

This would appear to give you a way of just working with the last, say 1mb.

Colin W.



More information about the Python-list mailing list