[Tutor] directory traversal help

Michael python at mrfab.info
Thu Jul 3 14:47:00 CEST 2008


I have modified an algorithm from the think like a python programmer 
book for traversing folders and printing the files in those folders. It 
works for my original purposes but I have a students that wants to use 
it to run from a root folder, problem is that it crashes on the 
recycling bin as well as hidden and other types of folders. Is there a 
way to modify it to skip folders that would make it crash? I have tried 
using exception handling (try) and other functions in the os module but 
I cannot work it out. Any ideas? thanks


import os
import string

def walk(dir):
    for name in os.listdir(dir):
        path = os.path.join(dir,name)
        if os.path.isfile(path):
            beg = string.rfind(path,'\Student')
            end = len(path)
            filename = path[beg:end]
            #print "___________ ",filename
            print "___________ ",name

            print path
            walk (path)

cwd = os.getcwd()

More information about the Tutor mailing list