Skip to content

Instantly share code, notes, and snippets.

@wynemo
Created May 6, 2012 14:01
Show Gist options
  • Save wynemo/2622441 to your computer and use it in GitHub Desktop.
Save wynemo/2622441 to your computer and use it in GitHub Desktop.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment