Python遍历目录的4种方法
1.os.popen运行shell列表命令
def traverseDirByShell(path):
for f in os.popen('ls ' + path):
print f.strip()
2.利用glob模块
glob.glob(path)返回带目录的文件名.通配符和shell相似.path不能包含shell变量.
def traverseDirByGlob(path):
path = os.path.expanduser(path)
for f in glob(path + '/*'):
print f.strip()
3.利用os.listdir(推荐)
该方法返回不带根目录的文件名或子目录名
def traverseDirByListdir(path):
path = os.path.expanduser(path)
for f in os.listdir(path):
print f.strip()
4.利用os.walk(推荐)
返回一个包含3个项目的元组:当前目录名称,子目录名称,子文件名称
def traverseDirByOSWalk(path):
path = os.path.expanduser(path)
for (dirname, subdir, subfile) in os.walk(path):
#print('dirname is %s, subdir is %s, subfile is %s' % (dirname, subdir, subfile))
print('[' + dirname + ']')
for f in subfile:
print(os.path.join(dirname, f))
整合代码:
#!/usr/bin/python
import os
from glob import glob
def printSeparator(func):
def deco(path):
print("call method %s, result is:" % func.__name__)
print("-" * 40)
func(path)
print("=" * 40)
return deco
@printSeparator
def traverseDirByShell(path):
for f in os.popen('ls ' + path):
print f.strip()
@printSeparator
def traverseDirByGlob(path):
path = os.path.expanduser(path)
for f in glob(path + '/*'):
print f.strip()
@printSeparator
def traverseDirByListdir(path):
path = os.path.expanduser(path)
for f in os.listdir(path):
print f.strip()
@printSeparator
def traverseDirByOSWalk(path):
path = os.path.expanduser(path)
for (dirname, subdir, subfile) in os.walk(path):
#print('dirname is %s, subdir is %s, subfile is %s' % (dirname, subdir, subfile))
print('[' + dirname + ']')
for f in subfile:
print(os.path.join(dirname, f))
if __name__ == '__main__':
path = r'~/src/py'
traverseDirByGlob(path)
traverseDirByGlob(path)
traverseDirByListdir(path)
traverseDirByOSWalk(path)
代码节选自codego.net
转载于:https://blog.51cto.com/qianc81115/1631761
Python遍历目录的4种方法相关推荐
- python遍历数组的两种方法及将print的内容写入文件中
python遍历数组的两种方法 第一种,最常用的,通过for in遍历数组 colours = ["red","green","blue"] ...
- python3(4)--- python遍历数组的两种方法
python遍历数组的两种方法 第一种,最常用的,通过for in遍历数组.其实本质就是取出来的是这个数组或者列表中的元素,和角标无关. colours = ["red",&quo ...
- python遍历数组的两种方法的代码
工作过程中,把开发过程中较好的一些内容段备份一下,下面内容是关于python遍历数组的两种方法的内容,希望对小伙伴有用途. colours = ["red","green ...
- python 遍历数组的3种方法及常用的代码
1.直接使用数组的元素进行遍历,但这种遍历方法效率较低,不推荐使用.可以使用 python中的 num或者 int数组. 2.使用 python中的my_num,该方法通过数组的末尾进行遍历,效率最高 ...
- python遍历字典的5种方法
python遍历字典5种方法 方法一: 使用字典的键进行遍历. dict={"张三":90,'王五':87,'李四':99} for key in dict:print(" ...
- python遍历数组的两种方法加条件输出数组的值
第一种方法 1.通过for in遍历数组. 其实本质就是取出来的是这个数组或者列表中的元素,和角标无关. for predict_scores in predict_scores:print(pred ...
- python 遍历目录_Python遍历目录的4种方法实例介绍
1.os.popen运行shell列表命令 复制代码 代码如下: def traverseDirByShell(path): for f in os.popen('ls ' + path): prin ...
- python遍历目录_Python遍历目录的4种方法
1.os.popen运行shell列表命令 def traverseDirByShell(path): for f in os.popen('ls ' + path): print f.strip() ...
- python遍历字典的四种方法
根据key值遍历 >>> a {'a': '1', 'b': '2', 'c': '3'} >>> for key in a:print(key+':'+a[key ...
最新文章
- c语言多线程mysql_多线程读写mysql数据库
- C言语教程第五章:函数(1)
- [JAVA基础] 成员变量和局部变量(一看就懂的总结归纳篇)
- LINUX系统管理命令
- java的数列极差_[hoj]数列极差问题 | 学步园
- cordova 支付_Cordova打包教程(H5项目打包成ipa)
- mysql8.0安装问题
- 51nod 1274 最长递增路径(DP)
- 数据结构(线性表)——神奇的幻方
- 【机器人操作系统】ROS工作空间及功能包的创建
- 反向传播与梯度消失梯度爆炸
- 英文单词乱序后还原 c语言,乱序背单词_有谁也用《不背单词》这个软件吗怎么可以改变背单词的顺序有顺序倒序和乱序的_淘题吧...
- php匹配ubb,UBB类 php UBB 解析实现代码
- Router入门0x205: react-route + redux + react 集成
- HTML中加入背景音乐
- EasySwoole3 Crontab的使用
- 微信小程序刷新页面数据
- 查看java安装路径[linux]
- 【Node】Error: ENOENT: no such file or directory,解决方案
- 电子技术——反馈对放大器极点的影响