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. 通过遍历可以实现多个文件自动合并
相关推荐
小田学Python5 小时前
100行Python代码,搭一个能干活的AI Agent
python·langchain·大模型·ai agent
Csvn5 小时前
🐍 Day3 : Python 容器精讲 — list、dict、set、tuple 底层实现与高级操作
后端·python
weixin-a153003083166 小时前
python-装饰器
开发语言·python
我的xiaodoujiao6 小时前
快速学习Python基础知识详细图文教程17--类型注解和断点调试
开发语言·python·学习·测试工具
每天吃饭的羊7 小时前
Chrome DevTools MCP
python
水獭比特9 小时前
localhost 不是安全边界:给 Agent Web 入口补上四层门禁
人工智能·python
Generalzy9 小时前
Whisper + VAD + TTS:一套完整的 Python 本地语音处理流水线
python·whisper·语音识别
qpsj9 小时前
让 LLM 控制 AutoCAD/ZWCAD:COM 自动化 + MCP 封装
python·llm
赟爸9 小时前
直播切片素材杂乱不好复用,易元AI要怎么处理
大数据·人工智能·python
怪奇云呼军10 小时前
从声音特征到 CRM 回流:闪电智能 Voice Agent 沟通策略自适应系统 v1 实战
android·人工智能·python·音视频·语音识别