Traveler wrote: > yes this is great i will work from that but how can i use say a list > to pass 10 words? > > mylist = ['word1','word2','word3','word4'] ... > >for root, dirs, files in os.walk('~/mydir'): > > for file in [f for f in files if f.endswith(".txt")]: > > fh = open(file) > > for line in fh: > > # Search for words. > > fh.close()
The following will allow you to search a line of text for one of a list of words. import re line = "line of text" mylist = ["bogus", "text", "here"] p = re.compile(r"\b(%s)\b" % '|'.join(mylist)) m = p.search(line) if m: print "Found %s" % m.group(1) Alternatively, the following will give a list of all words in a string that appear in the list: words_found = [word for word in re.split(r"\W+", line) if word in mylist] -- http://mail.python.org/mailman/listinfo/python-list