Smaple of recursive directory walker

Traveler Traveler at time.com
Sun Jul 30 17:25:23 EDT 2006


Thank you very much that is what i was looking for.


On 30 Jul 2006 13:18:51 -0700, "Ant" <antroy at gmail.com> wrote:

>
>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]




More information about the Python-list mailing list