首页 > 开发 > PHP > 正文

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

2024-05-04 21:50:06
字体:
来源:转载
供稿:网友
  1. #coding:utf-8 
  2. import os 
  3. #-------代码段一 
  4. #获取当前文件夹 
  5. filePath = os.getcwd() 
  6. #获取当前文件列表 
  7. fileNameList = os.listdir(filePath) 
  8. fileDirList = [] 
  9. #获取文件路径列表 
  10. for fileName in fileNameList: 
  11.   fileDirList.append(os.path.join(filePath, fileName)) 
  12. #--------代码段二 
  13. f = open('code.txt''w'
  14. f.write("开始写文件/n"
  15. f.close() 
  16. for code in fileDirList: 
  17.   f = open('code.txt''a'
  18.   split = os.path.splitext(code) 
  19.   if(split[1] == '.h' or split[1] == '.cpp'): 
  20.     fz = open(code, 'r'
  21.     string = "源代码文件" + code +"代码:/n" 
  22.     f.write(string) 
  23.     content = fz.read() 
  24.     print "文件 %s 读写成功" % code 
  25.     f.write(content) 
  26.     fz.close() 
  27.   f.close() 
  28. print "读写成功" 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表