How do I export the output of Python's built-in help() function

前端 未结 9 1084
时光取名叫无心
时光取名叫无心 2020-12-15 20:19

I\'ve got a python package which outputs considerable help text from: help(package)

I would like to export this help text to a file, in the format in w

9条回答
  •  误落风尘
    2020-12-15 20:25

    This is a bit hackish (and there's probably a better solution somewhere), but this works:

    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
    

    And then...

    >>> output_help_to_file(r'test.txt', 're')
    

提交回复
热议问题