当前位置: 移动技术网 > IT编程>脚本编程>Python > Python实现遍历目录的方法【测试可用】

Python实现遍历目录的方法【测试可用】

2017年12月12日  | 移动技术网IT编程  | 我要评论

儿茶,株洲长途汽车站,律政男神解读网贷新规

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《》

希望本文所述对大家Python程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网