import glob file_list = glob.glob('id2neg*.txt') # 获取文件夹下所有的txt文件 with open('merged_file.txt', 'w') as merged_file: for file_name in file_list: with open(file_name) as file: merged_file.write(file.read())