D35【python 接口自动化学习】- python基础之输入输出与文件操作

day35 文件合并

学习日期:20241012

学习目标:输入输出与文件操作﹣-47 如何使用python合并多个文件?

学习笔记:

合并文件需求分析
合并两个文件

代码实现

python 复制代码
# 合并两个文件
with open('demo1.txt') as f1:
    file_data_1=f1.read()

with open('demo2.txt') as f2:
    file_data_2=f2.read()

#将demo1和demo2的文件写入到demo3
with open('demo3.txt',mode='w') as f3:
    f3.write(file_data_1)
    f3.write(file_data_2)
遍历多个文件

代码实现

python 复制代码
# 合并多个文件
file_name=['demo1.txt','demo2.txt','demo3.txt']
file_data=[]

for f_name in file_name:
    with open(f_name) as f:
        file_data.append(f.read())

with open('demo4.txt',mode='w') as f:
    for data in file_data:
        f.write(data)
总结
  1. 文件的读,写是实现文件合并的基础
  2. 通过遍历可以实现多个文件自动合并
相关推荐
黄忠11 小时前
01-系统架构设计-LangGraph状态机与多源异构RAG
python
zzzzzz31011 小时前
假如我是掘金管理员,我先给评论区装个'代码审查'系统
python·程序员·机器人
砍材农夫12 小时前
python环境|conda安装和使用(2)
后端·python
程序员龙叔1 天前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
用户8356290780511 天前
使用 Python 操作 Word 内容控件
后端·python
码云骑士1 天前
32-慢查询排查全流程(下)-索引优化实战与最左前缀原则
python
闵孚龙1 天前
《PyTorch 深度修炼》Dataset 和 DataLoader:数据如何喂给模型
人工智能·pytorch·python
goldenrolan1 天前
A公司物料替代测试系统 v1.7:从需求到 exe/apk 的 AI 辅助全链路实践
android·自动化测试·软件测试·python·ai
菜板春1 天前
jupyter入门-手册-特征探索
python·jupyter