怎么将多个文件的内容进行合并(Talk is cheap, show me the code)

import os

def readfile(path): # 读取文件夹下所有的文件
    files = os.listdir(path)
    file_list = []
    for file in files:  # 遍历文件夹
        if not os.path.isdir(file):
            file_list.append(path + '/' + file)
    return file_list

def combine_main():
    clean_words = []
    file_list = readfile('combine_file') # 把所有的文件都放在combine_file文件夹下
    for file in file_list:
        with open(file,'r',encoding='utf-8') as f:
            for word in f.readlines():
                if str(word) not in clean_words:
                    clean_words.append(word)
    with open('combine_sentences.txt', 'a',encoding='utf-8') as f:
        for word in clean_words:
            f.write(word)

combine_main()

参考文档

猜你喜欢

转载自blog.csdn.net/weixin_44064649/article/details/103009876
me