需要重定向默认的标准输出流stdoutimport sys
#对输出流做备份
back = sys.stdout
filepath = 'out.txt'
with open(filepath,'w') as f:
sys.stdout = f
help(str)
#还原输出流
sys.stdout = back
打包成函数:
import sys
def help_to_file(filepath,content):
#对输出流做备份
back = sys.stdout
with open(filepath,'w') as f:
sys.stdout = f
help(content)
#还原输出流
sys.stdout = back
help_to_file('out.txt','str')