import os # 设置主文件夹路径 main_folder = "D://python_study//big_boss//doc//output" # 设置输出合并文件的路径 output_file = "D://python_study//big_boss//doc//output//all_processed.txt" # 打开合并后的文件以写入 with open(output_file, 'w', encoding='utf-8') as outfile: # 遍历主文件夹中的每个子文件夹 for root, dirs, files in os.walk(main_folder): # 如果当前文件夹中有 processed.txt 文件 if 'processed.txt' in files: folder_name = os.path.basename(root) # 获取当前子文件夹的名字 processed_file_path = os.path.join(root, 'processed.txt') # 读取 processed.txt 文件的内容并写入合并文件 with open(processed_file_path, 'r', encoding='utf-8') as infile: content = infile.read() outfile.write("《" + folder_name + "》" + "\n") # 添加换行符分隔每个文件内容 outfile.write(content + "\n\n\n") # 添加换行符分隔每个文件内容 print(f"所有文件的内容已经合并到 {output_file}。")