Python通过递归获取目录下指定文件代码实例
作者:Mars.wang 时间:2021-02-24 15:08:51
这篇文章主要介绍了python通过递归获取目录下指定文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
获取一个目录下所有指定格式的文件是实际生产中常见需求.
import os
#递归获取一个目录下所有的指定格式的文件
def get_jsonfile(path,file_list):
dir_list=os.listdir(path)
for x in dir_list:
new_x=os.path.join(path,x)
if os.path.isdir(new_x):
get_jsonfile(new_x,file_list)
else:
file_tuple=os.path.splitext(new_x)
if file_tuple[1]=='.json':
file_list.append(new_x)
return file_list
if __name__=='__main__':
file_list=[]
path='/Users/binwang/Documents'
get_jsonfile(path,file_list)
for json_file in file_list:
print(json_file)
来源:https://www.cnblogs.com/wangbin2188/p/11811866.html
标签:python,递归,获取,目录,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
js全选/全不选/反选 checkbox代码
2008-03-18 13:00:00
网站的新趋势
2008-12-11 13:36:00
python实现人脸识别经典算法(一) 特征脸法
2021-07-15 11:35:03
像聪明女孩穿衣服那样设计网页文字
2007-11-06 16:45:00
![](https://img.aspxhome.com/file/UploadPic/200711/6/2007116164842924s.jpg)
set rs=server.CreateObject("adodb.recordset") 的中文详细说明
2011-03-06 11:21:00
python重试装饰器的简单实现方法
2022-07-15 12:54:10
Python+Tkinter制作猜灯谜小游戏
2021-09-24 19:43:17
![](https://img.aspxhome.com/file/2023/7/66887_0s.gif)
wxPython之解决闪烁的问题
2022-05-12 13:21:30
![](https://img.aspxhome.com/file/2023/1/86361_0s.jpg)
Python 并行化执行详细解析
2021-09-23 22:20:52
![](https://img.aspxhome.com/file/2023/0/70680_0s.png)
跟老齐学Python之list和str比较
2021-02-26 22:36:36
redis不能访问本机真实ip地址的解决方案
2023-07-05 13:09:44
Pytest mark使用实例及原理解析
2021-02-27 14:30:11
oracle下一条SQL语句的优化过程(比较详细)
2010-04-22 16:58:00
mssql 大小写区分方法
2008-12-29 14:08:00
JavaScript版无组件上传类
2007-10-06 23:16:00
apache+mysql+php+ssl服务器之完全安装攻略
2023-11-16 07:34:16
python next()和iter()函数原理解析
2023-04-05 21:58:50
python调用pyaudio使用麦克风录制wav声音文件的教程
2023-12-16 13:32:15
对python中的装包与解包实例详解
2022-05-08 11:57:59
Python 京东云无线宝消息推送功能
2021-08-19 09:45:57