Suppose you have a repeating directory tree, e.g.:

  • Folder 1
    • Subdir
  • Folder 2
    • Subdir
  • Folder 3
    • Subdir


You need to find all the files in each of the sub-directories and these subdirectories happen to share the same name, Subdir. Then you could use this python function to locate them.

import os, fnmatch 
 
def locate(pattern, root=os.curdir, subdir): 
        for path, dirs, files in os.walk(os.path.abspath(root)): 
                for filename in fnmatch.filter(files, pattern): 
                        if subdir in path: 
                                yield os.path.join(path, filename)


This a variation of the locate function posted by Simon Brunning. See it on ActiveState.

Tags: