Search code examples
pythonwildcardglob

Does python's glob function support wildcards with variable depth?


I'm writing a python script that uses this awkward glob syntax.

import glob    
F = glob.glob('./www.dmoz.org/Science/Environment/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/*/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/*/*/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/*/*/*/index.html')

Seems like there ought to be a way to wrap this is one line:

F = glob.glob('./www.dmoz.org/Science/Environment/[super_wildcard]/index.html')

But I don't know what the appropriate super wildcard would be. Does such a thing exist?


Solution

  • Sorry - it does not. You will have to probably write few lines of code using os.walk:

    for root, dirs, files in os.walk('/starting/path/'):
        for myFile in files:
            if myFile == "index.html":
                print os.path.join(root, myFile)