[BangPypers] How to handle files efficiently in python

brijithp at gmail.com brijithp at gmail.com
Wed Mar 23 05:45:35 CET 2011


Hi,

I too answered this way to this question. I thought there may be some other
efficient way to handle this situation.

*
Regrads,
Brijith P
****
* <brijith at nucoreindia.com>


On 22 March 2011 22:19, Noufal Ibrahim <noufal at gmail.com> wrote:

> On Tue, Mar 22 2011, brijithp at gmail.com wrote:
>
> > Hi All,
> >         How can I print last five lines of a file using python. The file
> may
> > contain thousands of lines each line may differ in length.
>
> [...]
>
> You'd have to use some kind of heuristic (a.k.a. dirty hack). Stat the
> file to find it's size and use an "average" length of line to go a few
> lines back. Then count the number of newlines from there to the end. If
> it's 4, then you have your 5 lines. Otherwise, seek back a little more
> and repeat.
>
> --
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list