Python 使用 OS 模块实现目录遍历
来源:互联网 发布:鲁班土建预算软件 编辑:程序博客网 时间:2024/06/05 04:15
Python 使用 OS 模块实现目录遍历
我的目录结构
也就是说我将要遍历的目录为: C:\\Users\\Eric\\Documents\\Pathon\\test
方法
- 函数的迭代
- 使用 os.walk() 方法
方法1: 函数的迭代
此处主要是想记录下一个知识点 — 即使用 函数默认值 实现 静态变量
完整代码
# _*_ coding: utf-8 _*_import os# 使用 函数默认值 实现 静态变量 ''' function: dirlist arguments: path -- 文件路径 allfiles -- 默认值, 记录目录的结构 output: allfiles -- 返回目录结构, 以列表的形式'''def dirlist(path, allfiles=[]): # 列出 path 下的所有文件 filelist = os.listdir(path) for filename in filelist: # 将文件名和路径 path 连接在一起, 比如文件中有一个文件夹是 f1, 那么 filecompletepath # 的结果是 C:\\Users\\Eric\\Documents\\Pathon\\test\\f1 filecompletepath = os.path.join(path, filename) allfiles.append(filecompletepath) # 判断 filecompletepath 是否是一个文件夹, 是的话再继续遍历 if os.path.isdir(filecompletepath): dirlist(filecompletepath) return allfilesallfiles = dirlist('C:\\Users\\Eric\\Documents\\Python\\test')for path in allfiles: print path
输出结果
C:\Users\Eric\Documents\Python\test\f1C:\Users\Eric\Documents\Python\test\f2C:\Users\Eric\Documents\Python\test\f3C:\Users\Eric\Documents\Python\test\jpgC:\Users\Eric\Documents\Python\test\jpg\aC:\Users\Eric\Documents\Python\test\jpg\getjpg.py
方法1 总结
开始我的代码是这样:
# diff 1: 少了默认参数 allfiles=[]def dirlist(path): filelist = os.listdir(path) # 将 allfiles = [] 放在这里 allfiles=[] for filename in filelist: filecompletepath = os.path.join(path, filename) allfiles.append(filecompletepath) if os.path.isdir(filecompletepath): dirlist(filecompletepath) return allfiles
最后的结果是:
C:\Users\Eric\Documents\Python\test\f1C:\Users\Eric\Documents\Python\test\f2C:\Users\Eric\Documents\Python\test\f3C:\Users\Eric\Documents\Python\test\jpg
** 得到这样的结果是因为: 当迭代函数 dirlist
的时候, allfiles = []
也会被执行,
所以最后某些路径不会被显示出来. 因此在这个时候突然想到, 应该用一个静态变量来实现自己
想要的效果 — 即每一次迭代不会将以前的结果消除, 可是 Pathon 并没有静态变量, 但是能用
函数的默认参数实现静态变量的功能, 对于更为细致的讲解, 可以看
Python使用函数默认值实现函数静态变量的方法 **
os.walk()
的输出结果
命令为:
for content in os.walk('C:\\Users\\Eric\\Documents\\Python\\test'): print content
结果是:
每一个 content
都是一个元组, 由 (path, directory, filename)
组成, 注意 directory
和 filename
均为 列表 .
方法2: 使用 os.walk()
完整代码
import osallfile = []# os.walk(path) 输出是一个 generator: for path, directory, filename in os.walk('C:\\Users\\Eric\\Documents\\Python\\test'): for direc in directory: allfile.append(os.path.join(path, direc)) for path in allfile: print path
输出结果
C:\Users\Eric\Documents\Python\test\f1C:\Users\Eric\Documents\Python\test\f2C:\Users\Eric\Documents\Python\test\f3C:\Users\Eric\Documents\Python\test\jpgC:\Users\Eric\Documents\Python\test\jpg\a
注意这里和上一个方法相比少了一个
C:\Users\Eric\Documents\Python\test\jpg\getjpg.py
不要在意细节!
方法2 总结
不错哦!
参考资料
- Python使用函数默认值实现函数静态变量的方法
0 0
- Python 使用 OS 模块实现目录遍历
- Python 使用os模块遍历文件目录
- python使用os模块的os.walk遍历文件夹
- Python os.walk 遍历目录
- python OS模块: 目录操作
- python os模块文件目录
- python os模块创建目录
- python os模块使用
- python os模块使用
- Python os模块使用
- python os模块使用
- python 下利用os模块创建目录以及巧妙使用if not os.path.exits()创建
- Python 用 os.walk 遍历目录
- Python 用 os.walk 遍历目录
- python OS-目录遍历-异常处理
- python--内建os.walk遍历目录
- Python中用os.walk 遍历目录
- Python 用 os.walk 遍历目录
- JavaScript权威指南_159_第17章_事件处理_17.1-事件类型-传统事件类型
- 100个 iOS 程序员面试题汇总
- 移动IM开发学习<5>
- 虚拟机Ubuntu下怎样访问Win7资源
- 32. JavaScript 调试
- Python 使用 OS 模块实现目录遍历
- Ember.js 入门指南——路由简介
- Bootstrap Themes & Templates
- OSPF不规则区域解决
- smarty模板三种变量来源
- 【Lintcode|leetcode】245 Subtree
- Opencv中HSV通道的图像的颜色判定
- Codeforces Round #320 (Div. 2) 579C A Problem about Polyline(数学)
- 自我探讨 国王和狱吏(合数)