1

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.

moka
  • 4,353
  • 2
  • 37
  • 63
  • The accepted answer to [this question][1] solved this particular problem for me. [1]: http://stackoverflow.com/questions/2909975/python-list-directory-subdirectory-and-files – Ngure Nyaga Nov 07 '12 at 12:15

2 Answers2

0

You already have this info, you're just not using it:

for root, dirnames, filenames in os.walk(path):
    # current folder: root
    # list of filenames in that folder: filenames
    # list of subdirectories in that folder: dirnames
    for filename in fnmatch.filter(filenames, name):
        matches.append(os.path.join(root, filename))
        #          current folder ----^       ^----- current file
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • I know that I have the folder names, it's more about the walking order. Basically I don't only want the paths, but want to create my own tree from it. – moka Dec 19 '11 at 11:05
0

May be you can store in a dictionary instead of a list. Try this and see if it fits your bill

matches = {}
for root, dirnames, filenames in os.walk(path):
    for filename in fnmatch.filter(filenames, name):
        matches.setdefault(root,[]).append(os.path.join(root, filename))
Abhijit
  • 62,056
  • 18
  • 131
  • 204