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. 通过遍历可以实现多个文件自动合并
相关推荐
zhaoyin19941 天前
关于文件读取中使用的斜杠问题
python
勇往直前plus1 天前
从文件到屏幕:Python/java 字符编码、解码、文本处理的底层逻辑解析
java·开发语言·python
~央千澈~1 天前
优雅草科技2026年2月重磅产品·优雅草·写作中枢 — 产品介绍与发布说明
python
Emotional。1 天前
2025 年度技术总结与规划:AI 时代的开发者成长之路
人工智能·python·ai·langchain
witAI1 天前
**AI仿真人剧制作软件2025推荐,解锁沉浸式数字内容创作
人工智能·python
Codefengfeng1 天前
Python Base环境中加包的方法
开发语言·python
清水白石0081 天前
《Python 编程全景解析:从核心精要到测试替身(Test Doubles)五大武器的实战淬炼》
开发语言·python
如若1231 天前
AutoDL云服务器 NVIDIA 570驱动 EGL渲染修复全记录
运维·服务器·python
甲枫叶1 天前
【claude】Claude Code正式引入Git Worktree原生支持:Agent全面实现并行独立工作
java·人工智能·git·python·ai编程
清水白石0081 天前
《Python 编程全景解析:从核心精要到 Hypothesis 属性基测试的边界探索》
开发语言·python