On 5 Jun 2006 10:01:06 -0700, PipedreamerGrey <[EMAIL PROTECTED]> wrote: > 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 > > -- > http://mail.python.org/mailman/listinfo/python-list > How about something like: import os, stat
class DirectoryWalker: # a forward iterator that traverses a directory tree, and # returns the filename def __init__(self, directory): self.stack = [directory] self.files = [] self.index = 0 def __getitem__(self, index): while 1: try: file = self.files[self.index] self.index = self.index + 1 except IndexError: # pop next directory from stack self.directory = self.stack.pop() self.files = os.listdir(self.directory) self.index = 0 else: # got a filename fullname = os.path.join(self.directory, file) if os.path.isdir(fullname) and not os.path.islink(fullname): self.stack.append(fullname) else: return fullname for file, st in DirectoryWalker("."): your function here not tested Lou -- Artificial Intelligence is no match for Natural Stupidity -- http://mail.python.org/mailman/listinfo/python-list