Let's use explore()
function from here
to save result of os.walk()
into dictionary.
And after that just iterate over names and match them with pattern.
My folders:
.\all_data
.\all_data\sub1
.\all_data\sub1\subsub1
.\all_data\sub1\subsub1\some_files
.\all_data\sub1\subsub2
.\all_data\sub2
def explore(starting_path):
alld = {'': {}}
for dirpath, dirnames, filenames in os.walk(starting_path):
d = alld
dirpath = dirpath[len(starting_path):]
for subd in dirpath.split(os.sep):
based = d
d = d[subd]
if dirnames:
for dn in dirnames:
d[dn] = {}
else:
based[subd] = filenames
return alld['']
data = explore('.')
for k, v in data['all_data'].iteritems():
if v:
for key in v:
if 'subsub' in key:
print key
>>> {'all_data': {'sub1': {'subsub1': {'some_files': []}, 'subsub2': []},
'sub2': []}}
>>> subsub2
>>> subsub1
You could use more smarter verifications here if 'subsub' in key:
as regex and so on.