遍历文件夹和文件

from os.path import basename, isdir
from os import listdir

def traverse(path, depth=0):
    prefix =  depth* '| ' + '|_'
    if(isdir(path)):
        print prefix, basename(path)
        for item in listdir(path):
            traverse(path+'/'+item, depth+1)
    else:
        print prefix, basename(path)

if __name__ == '__main__':
 traverse('./')

你可能感兴趣的:(遍历文件夹和文件)