python合并两个txt文件,最简洁版本

# coding:utf-8;

import os


def merge_file(path):
    files = os.listdir(path)
    print(files)
    outfile = 'result.txt'
    """a+是可以写也可以在文件后面追加,即在合并两个文件"""
    k = open(path + outfile, 'a+')
    # org_file_path = []
    for file in files:
        org_file = path + file
        # org_file_path.append(org_file)
        f = open(org_file)
        print(f.read())
        """换行写入新生成的文件"""
        k.write(f.read() + '\n')
    k.close()
    print('合并完成')


if __name__ == "__main__":
    path = r'C:\Users\think\Desktop\6\\'
    merge_file(path)

你可能感兴趣的:(python)