首页 > 解决方案 > 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 是不相等的数据帧

代码在没有抛出任何错误的情况下执行,但是当我检查日志文件时,它不包含打印的语句。

如何更正代码?

标签: python

解决方案


print默认情况下不刷新。查看python手册发现有flush关键字arg,或者干脆关闭文件。log_file.close()


推荐阅读