本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python URL操作技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

点赞(127)

评论列表共有 0 条评论

立即
投稿
返回
顶部