I really like phihag's answer. I adapted it to suit my needs.
import fnmatch,glob
def fileNamesRetrieve( top, maxDepth, fnMask ):
someFiles = []
for d in range( 1, maxDepth+1 ):
maxGlob = "/".join( "*" * d )
topGlob = os.path.join( top, maxGlob )
allFiles = glob.glob( topGlob )
someFiles.extend( [ f for f in allFiles if fnmatch.fnmatch( os.path.basename( f ), fnMask ) ] )
return someFiles
I guess I could also make it a generator with something like this:
def fileNamesRetrieve( top, maxDepth, fnMask ):
for d in range( 1, maxDepth+1 ):
maxGlob = "/".join( "*" * d )
topGlob = os.path.join( top, maxGlob )
allFiles = glob.glob( topGlob )
if fnmatch.fnmatch( os.path.basename( f ), fnMask ):
yield f
Critique welcome.