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

前端 未结 9 1044
时光取名叫无心
时光取名叫无心 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:40

    pydoc.render_doc(thing) to get thing's help text as a string. Other parts of pydoc like pydoc.text and pydoc.html can help you write it to a file.

    Using the -w modifier in linux will write the output to a html in the current directory, for example;

    pydoc -w Rpi.GPIO
    

    Puts all the help() text that would be presented from the command help(Rpi.GPIO) into a nicely formatted file Rpi.GPIO.html, in the current directory of the shell

    0 讨论(0)
  • 2020-12-15 20:41

    An old question but the newer recommended generic solution (for Python 3.4+) for writing the output of functions that print() to terminal is using contextlib.redirect_stdout:

    import contextlib
    
    def write_help(func, out_file):
        with open(out_file, 'w') as f:
            with contextlib.redirect_stdout(f):
                help(func)
    

    Usage example:

    write_help(int, 'test.txt')
    
    0 讨论(0)
  • 2020-12-15 20:43

    The simplest way to do that is via using

    sys module

    it opens a data stream between the operation system and it's self , it grab the data from the help module then save it in external file

    file="str.txt";file1="list.txt"
    out=sys.stdout
    sys.stdout=open('str_document','w')
    help(str)
    sys.stdout.close
    
    0 讨论(0)
提交回复
热议问题