import os def read_files_recursively(directory, output_filename="llama.txt"): """ 递归读取指定目录下的所有文件,并将它们的内容按顺序写入一个新文件中。 每个文件的内容以文件名和相对路径作为注释开头。 """ script_filename = os.path.basename(__file__) # 获取当前脚本的文件名 with open(output_filename, "w", encoding="utf-8") as outfile: for root, dirs, files in os.walk(directory): for filename in sorted(files): if filename != script_filename: filepath = os.path.join(root, filename) relative_path = os.path.relpath(filepath, directory) outfile.write(f"### {relative_path} ###\n") with open(filepath, "r", encoding="utf-8") as infile: outfile.write(infile.read()) outfile.write("\n\n") if __name__ == "__main__": read_files_recursively(".") # 从当前目录开始递归读取