How to use os.path.walk method

Benjamin Derstine bend at realeducation.com
Tue Apr 6 17:15:11 EDT 1999


Can anyone give me a brief example of how to use this method?  The
documentation is somewhat unclear:

walk (path, visit, arg)
     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). The argument dirname specifies the visited directory, the
argument names lists the files in the directory (gotten from
os.listdir(dirname)). The visit function may modify names to influence the
set of directories visited below dirname, e.g., to avoid visiting certain
parts of the tree. (The object referred to by names must be modified in
place, using del or slice assignment.)

I understand I need to define a second function for the visit argument but
I'm unclear as to the third argument (arg) in walk() is for.  Likewise with
the first argument in visit().  I tried a dummy function just to print the
directories it walks like so:

import os

def visit(something, dirname, names):
     print dirname
     print names
     print something

arg=None
os.path.walk('/Windmodem/',visit,arg)

where 'Windowmodem' is a directory on my root.

But this does nothing and quits without returning any errors.
Thanks,

Ben








More information about the Python-list mailing list