Search code examples
pythonpathwildcardglob

Python Glob.glob: a wildcard for the number of directories between the root and the destination


Okay I'm having trouble not only with the problem itself but even with trying to explain my question. I have a directory tree consisting of about 7 iterations, so: rootdir/a/b/c/d/e/f/destinationdir

The thing is some may have 5 subdirectory levels and some may have as many as ten, such as:

rootdir/a/b/c/d/destinationdir

or:

rootdir/a/b/c/d/e/f/g/h/destinationdir

The only thing they have in common is that the destination directory is always named the same thing. The way I'm using the glob function is as follows:

for path in glob.glob('/rootdir/*/*/*/*/*/*/destinationdir'):
--- os.system('cd {0}; do whatever'.format(path))

However, this only works for the directories with that precise number of intermediate subdirectories. Is there any way for me not to have to specify that number of subdirectories(asterices); in other words having the function arrive at the destinationdir no matter what the number of intermediate subdirectories is, and allowing me to iterate through them. Thanks a lot!


Solution

  • I think this could be done more easily with os.walk:

    def find_files(root,filename):
        for directory,subdirs,files in os.walk(root):
            if filename in files:
                yield os.join(root,directory,filename)
    

    Of course, this doesn't allow you to have a glob expression in the filename portion, but you could check that stuff using regex or fnmatch.

    EDIT

    Or to find a directory:

    def find_files(root,d):
        for directory,subdirs,files in os.walk(root):
            if d in subdirs:
                yield os.join(root,directory,d)