efficient text file search.

George Sakkis george.sakkis at gmail.com
Mon Sep 11 14:36:22 EDT 2006


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