I am trying to use pythons os.walk to glob files from an arbitrary directory structure:
matches = []
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, name):
matches.append(os.path.join(root, filename))
print matches
this nicely globs all the files, but I'd like to also be able to maintain the actual folder structure, i.e. i'd like to walk subfolder by subfolder so I actually know what filename belongs to what subfolder. is that possible with os.walk or will I have to roll my own function?
To clarify my question: I want to create a program internal tree of the directories I parse so I'd like to actually parse the directory tree in a recursive fashion so that I come across each subdirectory and file just once, i.e:
pseudocode:
def createAndParseSubDir(path):
ret = []
files = glob(path)
for file in files:
if isDir(file):
ret.append(createAndParseSubDir(file))
else:
ret.append(file)
return ret
hierarchy = createAndParseSubDir(myRoot)
EDIT: I ended up using a similar recursive function like the pseudocode above to ensure that the files/dirs are parsed in a tree like manner.