Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 735
Abe Avatar asked Jul 30 '11 23:07

Abe


3 Answers

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)
like image 135
Michał Šrajer Avatar answered Oct 30 '22 19:10

Michał Šrajer


I have just released Formic which implements exactly the wildcard you need - '**' - in an implementation of Apache Ant's FileSet and Globs.

The search can be implemented:

import formic
fileset = formic.FileSet(include="/www.dmoz.org/Science/Environment/**/index.html")
for file_name in fileset.qualified_files():
    # Do something with file_name

This will search from the current directory. I hope this helps.

like image 34
Andrew Alcock Avatar answered Oct 30 '22 19:10

Andrew Alcock


I don't know if this is new, but glob CAN do this now.

For example,

F = glob.glob('./www.dmoz.org/Science/Environment/**/index.html', recursive=True)
like image 32
JacKeown Avatar answered Oct 30 '22 21:10

JacKeown