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. 通过遍历可以实现多个文件自动合并
相关推荐
CeshirenTester8 小时前
9B 上端侧:多模态实时对话,难点其实在“流”
开发语言·人工智能·python·prompt·测试用例
Starry_hello world8 小时前
Python (2)
python
ID_180079054738 小时前
Python爬取京东商品库存数据与价格监控
jvm·python·oracle
-To be number.wan9 小时前
Python数据分析:时间序列数据分析
开发语言·python·数据分析
Faker66363aaa9 小时前
YOLO13-C3K2-AdditiveBlock:水果质量智能检测系统_3
python
2401_8288906410 小时前
实现扩散模型 Stable Diffusion - MNIST 数据集
人工智能·python·深度学习·stable diffusion
jz_ddk10 小时前
[指南] Python循环语句完全指南
开发语言·python·continue·循环·for·while·break
Evand J10 小时前
【Python代码例程】长短期记忆网络(LSTM)和无迹卡尔曼滤波(UKF)的结合,处理复杂非线性系统和时间序列数据
python·lstm·滤波
workflower11 小时前
易用性和人性化需求
java·python·测试用例·需求分析·big data·软件需求
嚯嚯歪11 小时前
攻克腾讯 TCaptcha 滑块验证码:纯 HTTP 协议逆向实战
爬虫·python·逆向·验证码识别