[BangPypers] How to handle files efficiently in python

Senthil Kumaran orsenthil at gmail.com
Thu Mar 24 03:26:45 CET 2011


On Thu, Mar 24, 2011 at 02:25:04AM +0530, Vishal wrote:
> if you could read the entire file in one go...(i.e. unless your file is more
> than 50MB)...how about the following?

>>> for line in reversed((open('filename').readlines()[-1:-n:-1])):
...     print line

Some comments:

> # n is the number of lines you want to read.
> l = open(filename).read().rsplit('\n', n+1)

- readlines would be better.

> # 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

- Could not get why you are setting the first item to None.

> gc.collect()

This does not free anything. Where is something un-referenced for it
to garbage collect?

-- 
Senthil


More information about the BangPypers mailing list