/ Published in: Python
URL: http://www.faqts.com/knowledge_base/view.phtml/aid/2682/fid/245
Expand |
Embed | Plain Text
import os import fnmatch class GlobDirWalker: # lpt http://www.faqts.com/knowledge_base/view.phtml/aid/2682/fid/245 # a forward iterator that traverses a directory tree def __init__(self, directory, pattern="*"): self.stack = [directory] self.pattern = pattern 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) if fnmatch.fnmatch(file, self.pattern): return fullname
You need to login to post a comment.
