Python实现文件内容批量追加的方法示例
作者:我才是二亮 时间:2021-11-13 13:47:40
本文实例讲述了Python实现文件内容批量追加的方法。分享给大家供大家参考,具体如下:
#coding:utf-8
import os
#-------代码段一
#获取当前文件夹
filePath = os.getcwd()
#获取当前文件列表
fileNameList = os.listdir(filePath)
fileDirList = []
#获取文件路径列表
for fileName in fileNameList:
fileDirList.append(os.path.join(filePath, fileName))
#--------代码段二
f = open('code.txt', 'w')
f.write("开始写文件\n")
f.close()
for code in fileDirList:
f = open('code.txt', 'a')
split = os.path.splitext(code)
if(split[1] == '.h' or split[1] == '.cpp'):
fz = open(code, 'r')
string = "源代码文件" + code +"代码:\n"
f.write(string)
content = fz.read()
print "文件 %s 读写成功" % code
f.write(content)
fz.close()
f.close()
print "读写成功"
希望本文所述对大家Python程序设计有所帮助。
来源:http://blog.csdn.net/w0cai5hi3r1/article/details/41016293
标签:Python,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python时间序列处理之ARIMA模型的使用讲解
2021-04-10 05:53:34
![](https://img.aspxhome.com/file/2023/0/76800_0s.png)
使用Python实现图像标记点的坐标输出功能
2022-10-31 16:15:06
![](https://img.aspxhome.com/file/2023/9/68839_0s.jpg)
Python基础教程之tcp socket编程详解及简单实例
2021-04-18 12:04:29
laravel 实现阿里云oss文件上传功能的示例
2023-06-13 20:39:26
![](https://img.aspxhome.com/file/2023/2/55532_0s.jpg)
深入理解Python装饰器
2023-11-05 13:01:43
利用python实现汉诺塔游戏
2021-02-19 03:03:45
![](https://img.aspxhome.com/file/2023/3/87783_0s.jpg)
Windows10下Tensorflow2.0 安装及环境配置教程(图文)
2022-03-31 04:02:28
![](https://img.aspxhome.com/file/2023/1/95541_0s.jpg)
解决python 出现unknown encoding: idna 的问题
2023-10-06 21:26:06
![](https://img.aspxhome.com/file/2023/1/95411_0s.jpg)
九宫格基本布局
2009-06-18 18:36:00
![](https://img.aspxhome.com/file/UploadPic/20096/18/01-34s.gif)
python statsmodel的使用
2021-04-22 13:38:13
![](https://img.aspxhome.com/file/2023/8/87958_0s.png)
pytorch实现CNN卷积神经网络
2023-07-04 20:23:06
Python中的套接字编程是什么?
2021-02-28 12:46:01
![](https://img.aspxhome.com/file/2023/9/89349_0s.png)
使用ODBC接口访问MySQL
2009-02-13 13:59:00
Python面向对象类继承和组合实例分析
2021-08-25 05:08:12
asp 实现检测字符串是否为纯字母和数字组合的函数
2009-10-04 20:39:00
Python常用数据分析模块原理解析
2023-07-12 03:46:31
![](https://img.aspxhome.com/file/2023/5/107005_0s.jpg)
Python爬虫程序架构和运行流程原理解析
2023-10-04 16:04:24
![](https://img.aspxhome.com/file/2023/7/79477_0s.png)
对python 多线程中的守护线程与join的用法详解
2021-08-11 10:56:51
基于PyQt5制作一个PDF文件合并器
2023-04-27 07:50:58
![](https://img.aspxhome.com/file/2023/8/112618_0s.png)
python爬虫豆瓣网的模拟登录实现
2023-07-08 20:28:08
![](https://img.aspxhome.com/file/2023/9/115439_0s.jpg)