wynemo
5/6/2012 - 2:01 PM

walk folder util

walk folder util

def walkutil(input_folder,folder_cbk=None,file_cbk=None):
    def walk(str1):#nestd function to walk dir
        import os,os.path
        str1 = os.path.abspath(str1)#get absolute path
        for file in [file for file in os.listdir(str1) if not file in [".",".."]]:
            nfile = os.path.join(str1,file)#combine path
            if os.path.isdir(nfile):#is folder
                if folder_cbk is not None:
                    folder_cbk(nfile)
                walk(nfile)#iter
            else:#or file
                nfile = nfile.replace('\\','/')#replace windows slash
                file1 = nfile.replace(input_folder + '/','',1)#get relative path
                if file_cbk is not None:
                    file_cbk(file1)
    walk(input_folder)


def folder_callback(folder_name):
    print folder_name

def file_callback(file_name):
    print file_name

def main():
    walkutil('.',folder_callback,file_callback)

if __name__ == '__main__':
    main()