Expanding Search to Subfolders

PipedreamerGrey pipedreamergrey at gmail.com
Mon Jun 5 13:01:06 EDT 2006


This is the beginning of a script that I wrote to open all the text
files in a single directory, then process the data in the text files
line by line into a single index file.

os.chdir("C:\\Python23\\programs\\filetree")
mydir = glob.glob("*.txt")

index = open("index.rtf", 'w')

for File in mydir:
    count = 1
    file = open(File)
    fileContent = file.readlines()
    for line in fileContent:
        if not line.startswith("\n"):
            if count == 1:

I'm now trying to the program to process all the text files in
subdirectories, so that I don't have to run the script more than once.
I know that the following script will SHOW me the contents of the
subdirectories, but I can't integrate the two:

def print_tree(tree_root_dir):
    def printall(junk, dirpath, namelist):
        for name in namelist:
            print os.path.join(dirpath, name)
    os.path.walk(tree_root_dir, printall, None)

print_tree("C:\\Python23\\programs\\filetree")

I've taught myself out of online tutorials, so I think that this is a
matter of a command that I haven't learned rather a matter of logic.
Could someone tell me where to learn more about directory processes or
show me an improved version of my first script snippet?

Thanks




More information about the Python-list mailing list