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. 通过遍历可以实现多个文件自动合并
相关推荐
优秘UMI4 分钟前
大语言模型 (LLM):理解与生成内容的核心技术引擎
python·科技·其他·ai
sherlock_ye47 分钟前
‘jupyter‘ 不是内部或外部命令,也不是可运行的程序或批处理文件,最终解决方案!
ide·python·jupyter·conda
Salt_072819 分钟前
DAY27 pipeline管道
python·机器学习
萧鼎20 分钟前
Python PyWavelets(pywt)库完整技术指南:从小波理论到工程实践
开发语言·python
MediaTea27 分钟前
Python 装饰器:@property_name.deleter
开发语言·python
中等生27 分钟前
Celery 异步任务完全指南:从入门到实战
python·flask
Cherry的跨界思维30 分钟前
5、Python长图拼接终极指南:Pillow/OpenCV/ImageMagick三方案
javascript·python·opencv·webpack·django·pillow·pygame
acethanlic1 小时前
使用Ruff进行Python代码Format、lint和fix
python
codists1 小时前
在 Pycharm 中 debug Scrapy 项目
python
Pyeako1 小时前
操作HTML网页(PyCharm版)
爬虫·python·html