若要将程序的输出写入txt文件,可以在程序中使用Python内置函数open(): with open('filename.txt', 'w') as file: # 使用'w'来指定写模式 file.write(your_program_output) 另外,也可以添加上其他可选项以控制文件的输出格式,比如可以改为open('filename.txt', 'w', encoding='utf-8')来指定使用utf-8输出格式。