python提取一个目录下的json文件或其它格式的所有文件。

有时候,我们会遇到问题,比如需要提取一个具有多层级目录下的所有某些格式的文件,目录结构有好几层,里面的目录套着更多的目录。文件数量有几十到几万。我以json格式为例。

import os

import shutil

def copy_all_json_files(src_directory, dest_directory):

for root, dirs, files in os.walk(src_directory):

for file in files:

if file.endswith(".json"):

src_file = os.path.join(root, file)

dest_file = os.path.join(dest_directory, file)

shutil.copy2(src_file, dest_file)

指定源目录和目标目录路径

source_directory = "提取目录"

destination_directory = "输入目录"

复制所有 JSON 文件

copy_all_json_files(source_directory, destination_directory)

这样,我们就把这个目录下的所有json文件提取到我们的输入目录里面了,可以更换其它的文件格式。

相关推荐
2601_962078194 小时前
Python中calendar.weekday用法
python·编程技巧·calendar·日期处理·weekday
2601_962218614 小时前
万象生鲜系统业财一体化底层打通技术自动生成经营账单
大数据·数据库·人工智能·python·算法
2601_966949654 小时前
为什么量化策略需要大量历史股票数据?从回测可信度理解数据规模
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
2601_962885724 小时前
如何用 Python 扫描 A 股跳空缺口并统计缺口回补概率?
java·前端·python
李高钢4 小时前
Python FastAPI 框架入门:从零搭建你的第一个高性能 API 服务
数据库·python·fastapi
ocean21035 小时前
2025-2026年Python面试高频知识点洞察
开发语言·python·面试·python八股文
Warson_L5 小时前
Python的OrderedDict
python
隐擎fox5 小时前
高性能网络爬虫架构设计:基于 Python 的长连接复用与分布式会话池调度实践
分布式·python·网络协议·tcp/ip·高并发·网络爬虫、
Warson_L5 小时前
Python的TypedDict
python·langchain·llm
晓窗科技6 小时前
口碑好的AI基座服务商
大数据·人工智能·python