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. 通过遍历可以实现多个文件自动合并
相关推荐
MATLAB代码顾问2 分钟前
Python实现蜂群算法优化TSP问题
开发语言·python·算法
yaodong51814 分钟前
不会Python也能数据分析:Gemini 3.1 Pro解决办公问题的SQL自动生成
python·sql·数据分析
BU摆烂会噶27 分钟前
【LangGraph】持久化实现的三大能力——时间旅行
数据库·人工智能·python·postgresql·langchain
消失的旧时光-19431 小时前
统一并发模型:线程、Reactor、协程本质是一件事(从线程到协程 · 第6篇·终章)
java·python·算法
zhaoyong2223 小时前
MySQL 存储过程中字符集与排序规则不匹配导致查询性能下降的解决方案
jvm·数据库·python
sinat_383437363 小时前
golang如何从Python转型Go开发_golang从Python转型Go开发攻略
jvm·数据库·python
rockey6273 小时前
基于AScript的python3脚本语言发布啦!
python·c#·.net·script·python3·eval·expression·function·动态脚本
gqk013 小时前
Python入门
python
Muyuan19984 小时前
28.Paper RAG Agent 开发记录:修复 LLM Rerank 的解析、Fallback 与可验证性
linux·人工智能·windows·python·django·fastapi
代码小书生4 小时前
statistics,一个统计的 Python 库!
开发语言·python