Smaple of recursive directory walker

Ant antroy at gmail.com
Sat Jul 29 15:01:03 EDT 2006


> At work I have a directory of about 50 large text files and i need to
> search thru them for 10 separate words and print how many were found
> in total.
>
> I am new to python so could somebody please show me some sample code
> that would help me get this done and i will work from that.

Assuming it's primarily the directory walk you need help with,
something like the following should help:

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()




More information about the Python-list mailing list