如何重新修改web.py中的输出

Cle*_*rix 5 python web.py

关于web.py如何将输出重新修改为另一个输出目标,如日志文件或完全删除它?

Fer*_*yer 2

控制台输出被print发送到sys.stdout. 如果需要,您可以用打开的文件或您自己的类似文件的对象替换该流。唯一的要求是您的自定义对象有一个write()方法。

class MyOutputStream(object):

    def write(self, data):
        pass   # Ignore output

import sys
sys.stdout = MyOutputStream()

print("Test")  # Output is ignored
Run Code Online (Sandbox Code Playgroud)

如果要访问或恢复原始输出流,请使用sys.__stdout__.

sys.stdout = sys.__stdout__  # Restore stdout
Run Code Online (Sandbox Code Playgroud)