python 多個txt文件合併爲一個

import os

def saveFile(filename, content):
    f = open(filename, "w", encoding="utf-8")
    f.write(content)
   


def combinetxt(rootdir,filename):
    f = open(rootdir+filename, "w",encoding="utf-8")#創建文件
    list = os.listdir(rootdir) #枚舉文件夾下所有文件
    for i in range(0,len(list)):#遍歷
            path = os.path.join(rootdir,list[i])#完整路徑
            if os.path.isfile(path):#如果是文件
                content = readFile(path)#讀取所有
                f.write("*****************************")
                f.write(content)#寫入
    f.close();#關閉


def start():
    combinetxt("D:/t1","mygzcj.txt")
    combinetxt("D:/t2","mygzwyb.txt")


start()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章