用多种方式独立完成目录的遍历,目录的复制
2018-01-12 17:45
399 查看
#方法1 递归 import os def getAllDirAndFile(sourcePath): if not os.path.exists(sourcePath): return pathList=os.listdir(sourcePath) # print(pathList) for pathName in pathList: absPath=os.path.join(sourcePath,pathName) if os.path.isdir(absPath): getAllDirAndFile(absPath) if os.path.isfile(absPath): print(absPath) if __name__ == '__main__': sourcePath = r"F:\python\lianxi" getAllDirAndFile(sourcePath)
#方法2 广度遍历(队列) import os import collections def getAllDirAndFile(sourcePath): if not os.path.exists(sourcePath): return queue=collections.deque() queue.append(sourcePath) whi 4000 le True: if len(queue)==0: break path=queue.popleft() for pathName in os.listdir(path): absPath=os.path.join(path,pathName) if os.path.isdir(absPath): queue.append(absPath) if os.path.isfile(absPath): print(absPath) if __name__ == '__main__': sourcePath=r"F:\python\resource\day11" getAllDirAndFile(sourcePath)
相关文章推荐
- Python遍历目录的多种方式
- 遍历Map集合的多种方式
- python 遍历目录,复制指定文件
- java遍历文件夹并复制文件到指定目录
- 用非递归方式遍历目录及其子目录的文件
- MFC 遍历目录下指定类型的文件并复制
- 深复制和参数传递方式来完成数据修改操作
- Map集合,List集合,Array数组,Iterator迭代,Collection集合,Set集合的多种遍历方式
- 二叉树的多种遍历方式
- 复制一个文件夹下的所有文件(复制文件、遍历目录)
- Nginx+PHP防webshell跨站,跨目录的安全设置,多种方式,适合php5.3以上
- php递归方式遍历目录
- php遍历目录与文件夹的多种方法详解
- php遍历目录与文件夹的多种方法详解
- php通过递归方式复制目录和子目录的方法
- 遍历目录复制文件的代码
- php遍历目录与文件夹的多种方法详解
- C++ 递归方式复制文件夹目录
- PHP实现遍历某一目录下的所有文件并将特定后缀文件复制到特定目录
- JS遍历的多种方式