[BangPypers] How to handle files efficiently in python

Vishal vsapre80 at gmail.com
Wed Mar 23 21:55:04 CET 2011


On Wed, Mar 23, 2011 at 11:57 AM, Senthil Kumaran <orsenthil at gmail.com>wrote:

> On Wed, Mar 23, 2011 at 2:24 PM, Senthil Kumaran <orsenthil at gmail.com>
> wrote:
> > - file IO based. Seek to end of file and do a seek in the reverse
> >  direction based on a factor matching the size, and process and
> >  display. ( I found a good discussing on StackOverflow after writing
> >  this).
>
>
> http://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file-with-python-similar-to-tail
>
> --
> Senthil
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>

if you could read the entire file in one go...(i.e. unless your file is more
than 50MB)...how about the following?

# n is the number of lines you want to read.
l = open(filename).read().rsplit('\n', n+1)
# following is to keep the memory requirement low.
# but this is optional, if you only want to print the lines, and then end
the python process.
l[0] = None
gc.collect()
print '\n'.join(l[1:])
... do other stuff

if you cannot read the entire file in one go...then the stackoverflow answer
above from S.Lott is best.

Enjoy,
Vishal

-- 
Thanks and best regards,
Vishal Sapre

---
"So say...Day by day, in every way, I am getting better, better and better
!!!"
"A Strong and Positive attitude creates more miracles than anything else."
"Life is 10% how you make it, and 90% how you take it"
"Diamond is another piece of coal that did well under pressure”
"May we do good and not evil. May we find forgiveness for ourself and
forgive others. May we share freely, never taking more than we give."
"बहुजन हिताय, बहुजन सुखाय (Benefit for most people, Happiness for most
people.)"
The difference between "*ordinary*" and "*extraordinary*" is that "extra".
Its called by another name, "*preparation*". Extraordinary people prepare
themselves for situations that ordinary people choose to ignore.


More information about the BangPypers mailing list