faqts : Computers : Programming : Languages : Python : Common Problems : Data Structures

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

25 of 26 people (96%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

Walking recursivly through a directory-structure, I've been looking at os.path.walk for this, but I'm not sure how to implement it.

May 31st, 2000 09:33
Fred Drake, unknown unknown, bragib, François Pinard, Alex


You are on the right track... Just use os.path.walk(path, visit, arg)
You should write the visit function yourself.  Calls the function visit
with arguments (arg, dirname, names) for each directory in the directory
tree rooted at path (including path itself, if it is a directory).

So for instance if you would had a directory tree like this
/home/bragi/tmp2/
    /home/bragi/tmp2/file1.dat
    /home/bragi/tmp2/file2.dat
    /home/bragi/tmp2/file3.dat
      /home/brag/tmp2/dir2/file1.dat
      /home/bragi/tmp2/dir2/file2.dat

and you want to make a dictionary keyed by the directory name and the
dictionary values being the files in that dictionary.

import os
dirContents = {}
def visit(arg, dirname, names):
    files = filter(os.path.isfile, names)
    dirContents[dirname] = files
arg1 = 'Hello'
arg2 = 'World'
os.path.walk('/home/bragi/tmp2', visit, (arg1, arg2))
print dirContents

Your output would be:
{'/home/bragi/tmp2': ['file2.dat', 'file3.dat', 'file1.dat'],
'/home/bragi/tmp2/dir2': ['file2.dat', 'file1.dat']}

------

I'm sure examples abound. Like:

   http://www.iro.umontreal.ca/contrib/recode/lib/distroweb.py

Just look for occurrences of the `walk' or `walker' strings.

------

I wanted to learn to use os.path.walk, too, so I wrote a little function
that takes a directory and makes a graph out of all the files and
sub-directories below it:

import os

class Directory:

    def __init__ (self, path):
        self.path = path
        self.directory = {self.path: {}}
        self.directory_pointers = {self.path: self.directory[self.path]}
        os.path.walk (self.path, self.walk_callback, None)

    def walk_callback (self, args, directory, files):
        directory_list = self.directory_pointers[directory]
        self.directory_pointers[directory] = directory_list
        for file in files:
            file = os.path.join (directory, file)
            directory_list[file] = {}
            if os.path.isdir (file):
                self.directory_pointers[file] = directory_list[file]
                
if __name__ == '__main__':
    t = Directory ('/afs/athena.mit.edu/user/a/l/alex_c/mail')
    import pprint
    pprint.pprint (t.directory)