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

Stefan Behnel stefan_ml at behnel.de
Mon Feb 1 04:45:28 EST 2010


Stephen.Wu, 01.02.2010 10:17:
> 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?

Others have already pointed out that reading the entire file into memory is
not a good idea. Try reading chunks repeatedly instead.

As it appears that you simply try to find out if a file contains a specific
byte sequence, you might find acora interesting:

http://pypi.python.org/pypi/acora

Also note that there are usually platform optimised tools available to
search content in files, e.g. grep. It's basically impossible to beat their
raw speed even with hand-tuned Python code, so running the right tool using
the subprocess module might be a solution.

Stefan



More information about the Python-list mailing list