What?
在 Python 程序中,使用 print 输出调试信息的做法非常常见,但有的时候我们需要将 print 的内容改写到其他位置,比如一个文件中,便于随时排查。
但是又不希望大面积替换 print 函数,这就需要一些技巧实现。
一种典型的做法是在代码开始的地方增加这样的代码:
def log_to_file(* args):
# write all args to some a file
pass
print = log_to_file
修改 print 方法,之后的 print 实际上就不是内置方法了。
在 Linux 下也可以通过 shell 的输出重定向,将 print 的内容写入文件:
python3 your_script.py >> log.txt
其实在 Python 代码中,也可以通过输出重定向技术,直接实现上面的目的。
重定向 stdout
stdout 是系统的标准输出,通常是 shell 命令行。如果我们用其他对象覆盖 sys.stdout 就可以实现输出的重定向。
Python 使用任意实