How to use os.path.walk method

Al Christians achrist at easystreet.com
Tue Apr 6 18:05:17 EDT 1999


Are you sure that you've got that right?  I cut your code and
put it into a Python console, then changed the dir from 
/Windmodem/ to / and it worked for me.  Maybe you meant 'Winmodem'?

Al


Benjamin Derstine wrote:
> 
> 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