pycharm的print输出保存到txt文件

您所在的位置:网站首页 pycharm运行配置里要模拟输出控制台中的终端 pycharm的print输出保存到txt文件

pycharm的print输出保存到txt文件

2024-07-04 16:22| 来源: 网络整理| 查看: 265

程序比较大,运行时间又长的时候,不想等着结果,只要先确保程序功能正确无误,可以把需要的结果print然后保存到TXT文件中,超级方便

# 创建一个txt文件,文件名为mytxtfile def text_create(name): desktop_path = "C:\\Users\\Administrator\\PycharmProjects\\EmotionRecog\\venv\\Scripts\\src\\mylog\\" # 新创建的txt文件的存放路径 full_path = desktop_path + name + '.txt' # 也可以创建一个.doc的word文档 file = open(full_path, 'w') filename = 'log' text_create(filename) output = sys.stdout outputfile = open("C:\\Users\\Administrator\\PycharmProjects\\EmotionRecog\\venv\\Scripts\\src\\mylog\\" + filename + '.txt', 'w') sys.stdout = outputfile balabalabalabalabala # 运算部分 print('accuracy = ', accuracy*100, file=outputfile) outputfile.close() # close后才能看到写入的数据

重点是print里面要说明file=outputfile,并且程序末尾要关闭文件,不然都不会成功

展示一下 在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3