您有两种选择:
打开一个日志文件,并用它而不是函数替换sys.stdout:
log = open("myprog.log", "a")sys.stdout = log
print(“Hello”)
nothing is printed because it goes to the log file instead.
用您的日志功能替换打印:
# If you're using python 2.x, uncomment the next line
from future import print_function
print = log.info
print(“Hello!”)
nothing is printed because log.info is called instead of print



