how long a Str can be used in this python code segment?

Chris Rebert clp2 at rebertia.com
Mon Feb 1 04:26:55 EST 2010


On Mon, Feb 1, 2010 at 1:17 AM, Stephen.Wu <54wutong at 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.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list