这有点骇人听闻(并且某处可能有更好的解决方案),但这可行:
import sys
import pydoc
def output_help_to_file(filepath, request):
f = open(filepath, 'w')
sys.stdout = f
pydoc.help(request)
f.close()
sys.stdout = sys.__stdout__
return
接着…
>>> output_help_to_file(r'test.txt', 're')