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. 通过遍历可以实现多个文件自动合并
相关推荐
一人の梅雨4 小时前
中国制造网关键字搜索接口实战:跨境B2B视角的精准匹配与本地化适配方案
人工智能·python·制造
hahahahanhanhan4 小时前
Tensorflow使用GPU(cuda和cudnn和tensorflow下载)
人工智能·python·tensorflow·gpu
深度学习lover4 小时前
<数据集>yolo毛毛虫识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·毛毛虫识别
坐在地上想成仙4 小时前
从机床到键盘:用机械设计思维写出一个可部署网页
java·c++·python
Allen_LVyingbo4 小时前
用Python实现辅助病案首页主诊断编码:从数据清洗到模型上线(上)
开发语言·python·github·知识图谱·健康医疗
傻啦嘿哟4 小时前
Python家庭支出统计:从Excel到可视化图表的完整指南
开发语言·python·excel
子夜江寒4 小时前
OpenCV部分操作介绍
图像处理·python·opencv·计算机视觉
轻竹办公PPT4 小时前
2026 年 AI PPT 工具深度复盘:工具间的效率鸿沟与职场应用场景分析
人工智能·python·powerpoint
FJW0208144 小时前
Python装饰器
开发语言·python
Allen_LVyingbo5 小时前
用Python实现辅助病案首页主诊断编码:从数据清洗到模型上线(下)
开发语言·python·安全·搜索引擎·知识图谱·健康医疗