首页 > 解决方案 > 如何将python中的stdout重定向到函数?

问题描述

我知道我可以将标准输出重定向到一个文件:

stdout = open("file", "a")

但是我如何将它发送到 python 函数。我正在使用无头服务器,我想将输出重定向到易于监控的外部源。

我想要做:

def my_print_handler():
   # do something useful with the output other than printing it to a screen nobody sees

stdout = my_print_handler()

print ("Hello World") # sent to my_print_handler rather than screen

标签: pythonstdout

解决方案


推荐阅读