一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2017-07-17 编辑:简简单单 来源:一聚教程网

 
 代码如下复制代码
# *-* 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)
 

运行效果图如下:

热门栏目