本文實例講述了Python實現文件內容批量追加的方法。分享給大家供大家參考,具體如下:
#coding:utf-8import 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程序設計有所幫助。
新聞熱點
疑難解答