This may not solve your problem, but a while ago I needed to be able to recurse an unknown number of times through a directory structure to find a file, so I wrote this function:
import os
import fnmatch
def get_files(root, patterns='*', depth=1, yield_folders=False):
"""
Return all of the files matching patterns, in pattern.
Only search to teh specified depth
Arguments:
root - Top level directory for search
patterns - comma separated list of Unix style
wildcards to match NOTE: This is not
a regular expression.
depth - level of subdirectory search, default
1 level down
yield_folders - True folder names should be returned
default False
Returns:
generator of files meeting search criteria for all
patterns in argument patterns
"""
# Determine the depth of the root directory
root_depth = len(root.split(os.sep))
# Figure out what patterns we are matching
patterns = patterns.split(';')
for path, subdirs, files in os.walk(root):
# Are we including directories in search?
if yield_folders:
files.extend(subdirs)
files.sort()
for name in files:
for pattern in patterns:
# Determine if we've exceeded the depth of the
# search?
cur_depth = len(path.split(os.sep))
if (cur_depth - root_depth) > depth:
break
if fnmatch.fnmatch(name, pattern):
yield os.path.join(path, name)
break
With this function you could check to see if a file exists with the following:
checkdir = "/lib/modules/*/kernel/drivers/char"
matches = get_files(checkdir, depth=100, yield_folders=True)
found = True if matches else False
All this may be overkill, but it should work!