efficient text file search -solution

noro amit.man at gmail.com
Mon Sep 11 15:59:48 EDT 2006

OK, am not sure why, but

if fList.find('string') != -1:
   print 'FOUND'

works much much faster.

it is strange since i thought 'for line in file('somefile')' is
optemized and read pages to the memory,
i guess not..

George Sakkis wrote:
> noro wrote:
> > Is there a more efficient method to find a string in a text file then:
> >
> > f=file('somefile')
> > for line in f:
> >     if 'string' in line:
> >          print 'FOUND'
> >
> > ?
> Is this something you want to do only once for a given file ? The
> replies so far seem to imply so and in this case I doubt that you can
> do anything more efficient. OTOH, if the same file is to be searched
> repeatedly for different strings, an appropriate indexing scheme can
> speed things up considerably on average.
> George

More information about the Python-list mailing list