efficient text file search.

Kent Johnson kent at kentsjohnson.com
Mon Sep 11 08:03:38 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'

Probably better to read the whole file at once if it isn't too big:
f = file('somefile')
data = f.read()
if 'string' in data:
	print 'FOUND'



More information about the Python-list mailing list