os.walk()

rbt rbt at athop1.ath.vt.edu
Thu Feb 17 09:29:30 EST 2005


Could someone demonstrate the correct/proper way to use os.walk() to skip certain 
files and folders while walking a specified path? I've read the module docs and 
googled to no avail and posted here about other os.walk issues, but I think I need to 
back up to the basics or find another tool as this isn't going anywhere fast... I've 
tried this:

for root, dirs, files in os.walk(path, topdown=True):

     file_skip_list = ['file1', 'file2']
     dir_skip_list = ['dir1', 'dir2']

     for f in files:
         if f in file_skip_list
             files.remove(f)

     for d in dirs:
         if d in dir_skip_list:
             dirs.remove(d)

     NOW, ANALYZE THE FILES

And This:

     files = [f for f in files if f not in file_skip_list]
     dirs = [d for d in dirs if dir not in dir_skip_list]

     NOW, ANAYLZE THE FILES

The problem I run into is that some of the files and dirs are not removed while others 
are. I can be more specific and give exact examples if needed. On WinXP, 
'pagefile.sys' is always removed, while 'UsrClass.dat' is *never* removed, etc.



More information about the Python-list mailing list