脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现文件内容批量追加的方法示例

Python实现文件内容批量追加的方法示例

2020-12-04 00:49我才是二亮 Python

这篇文章主要介绍了Python实现文件内容批量追加的方法,结合实例形式分析了Python文件的读写相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python实现文件内容批量追加的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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

延伸 · 阅读

精彩推荐