Searching a file for multiple strings

gotbyrd gotbyrd at gmail.com
Sat Jan 31 14:12:16 EST 2009


Hello,

I'm fairly new with python and am trying to build a fairly simple
search script.  Ultimately, I'm wanting to search a directory of files
for multiple user inputted keywords.  I've already written a script
that can search for a single string through multiple files, now I just
need to adapt it to multiple strings.

I found a bit of code that's a good start:

import re
test = open('something.txt', 'r').read()

list = ['a', 'b', 'c']

foundit = re.compile('|'.join(re.escape(target) for target in list))
if foundit.findall(test):
    print 'yes!'

The only trouble with this is it returns yes! if it finds any of the
search items, and I only want a return when it finds all of them.  Is
there a bit of code that's similar that I can use?

Thanks



More information about the Python-list mailing list