From: MRAB on
Chris Rebert wrote:
> On Mon, Feb 1, 2010 at 1:17 AM, Stephen.Wu <54wutong(a)gmail.com> wrote:
>> tmp=file.read() (very huge file)
>> if targetStr in tmp:
>> print "find it"
>> else:
>> print "not find"
>> file.close()
>>
>> I checked if file.read() is huge to some extend, it doesn't work, but
>> could any give me some certain information on this prolbem?
>
> If the file's contents is larger than available memory, you'll get a
> MemoryError. To avoid this, you can read the file in by chunks (or if
> applicable, by lines) and see if each chunk/line matches.
>
If you're processing in chunks then you also need to consider the
possibility that what you're looking for crosses a chunk boundary, of
course. It's an easy case to miss! :-)
From: Antoine Pitrou on
Le Mon, 01 Feb 2010 01:33:09 -0800, Stephen.Wu a écrit :
>
> actually, I just use file.read(length) way, i just want to know what
> exactly para of length I should set, I'm afraid length doesn't equal to
> the amount of physical memory after trials...

There's no exact length you "should" set, just set something big enough
that looping doesn't add any noticeable overhead, but small enough that
it doesn't take too much memory. Something between 64kB and 1MB sounds
reasonable.