python - Python打印到文件功能没有打印到文件
问题描述
我正在使用带有输出到文件参数的打印函数。print 函数在 if 语句下。下面是代码
log_file = open("Src_files.log", 'w')
if count_1.equals(count_2) == False:
print('Error: Discrepancy with processed file. Count of records does not match with sources file', file=log_file)
Count_1 和 count_2 是不相等的数据帧
代码在没有抛出任何错误的情况下执行,但是当我检查日志文件时,它不包含打印的语句。
如何更正代码?
解决方案
print
默认情况下不刷新。查看python手册发现有flush
关键字arg,或者干脆关闭文件。log_file.close()
推荐阅读
- jinja2 - 如何在 Flask WTForms 中动态附加表单?
- javascript - 如何在发送表单之前检查具有反应组件状态的对象的数组是否具有重复值?
- r - Stargazer 删除了比指定更多的协变量
- java - 如何编写正则表达式以从字符串中提取两个小数位严格为零的小数?
- tensorflow - Keras,一个将 Softmax 层的输出转换为分类数组的层?
- javascript - 更新全局变量值 REACT.JS
- google-sheets - 如何根据googlesheet中的数据子集计算记录?
- c# - 在 Web 服务中出现错误“反序列化 System.Guid 类型的对象时出错。值 '1' 无法解析为类型 'Guid'。'。”
- batch-file - 为什么删除字符串中的子字符串不起作用。批
- r - 如何在我的相对丰度图上将低相对丰度微生物属分组为“其他”?