[Tutor] searching out keywords...

Jeff Shannon jeff@ccvcorp.com
Fri, 08 Mar 2002 12:13:33 -0800


> But the more immediate challenge is to properly accomodate
> 40MB files.  inp.readlines() will bring all 40MB into
> RAM and/or virtual memory which may or may not be a
> problem.  Let's assume that it is.
>
> One way around this is to provide a sizehint in as your
> argument to readlines(), which will limit the size of
> what's brought in.   .....

Another option, provided you're using a recent enough version of Python, is to
simply use xreadlines() :

for line in logfile.xreadlines():
    # do stuff

This will read through all lines in the file, one by one, but without loading them
all into memory.  I'm not positive what version added xreadlines(), but it's
available in 2.1 if not earlier.

Jeff Shannon
Technician/Programmer
Credit International