本文实例讲述了Python实现遍历目录的方法。分享给大家供大家参考,具体如下:
# *-* coding=gb2312 *-* import os.path import shutil def traveltree(curPath,count): if not os.path.exists(curPath): return if os.path.isfile(curPath): fileName =os.path.basename(curPath) print '\t' *count+ '├─' + fileName elif os.path.isdir(curPath): print '\t' *count+ '├─' + curPath pathlist =os.listdir(curPath) for aa in pathlist: traveltree(curPath +"\\"+aa,count+1) if __name__=='__main__': traveltree("C:\\py",1)
运行效果图如下:
希望本文所述对大家Python程序设计有所帮助。