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

Question:

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 which it’s displayed by help(package)

How might I go about this?

Asked By: ipmcc

||

Answers:

If you do help(help) you’ll see:

Help on _Helper in module site object:

class _Helper(__builtin__.object)
 |  Define the builtin 'help'.
 |  This is a wrapper around pydoc.help (with a twist).

[rest snipped]

So – you should be looking at the pydoc module – there’s going to be a method or methods that return what help(something) does as a string…

Answered By: Jon Clements

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')
Answered By: Michael0x2a

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

Answered By: Aaron Altman

In Windows, just open up a Windows Command Line window, go to the Lib subfolder of your Python installation, and type

python pydoc.py moduleName.memberName > c:myFoldermemberName.txt

to put the documentation for the property or method memberName in moduleName into the file memberName.txt. If you want an object further down the hierarchy of the module, just put more dots. For example

python pydoc.py wx.lib.agw.ultimatelistctrl > c:myFolderUltimateListCtrl.txt

to put the documentation on the UltimateListCtrl control in the agw package in the wxPython package into UltimateListCtrl.txt.

Answered By: JDMorganArkansas

pydoc already provides the needed feature, a very well-designed feature that all question-answering systems should have. The pydoc.Helper.init has an output object, all output being sent there. If you use your own output object, you can do whatever you want. For example:

class OUTPUT():

def __init__(self):
    self.results = []
def write(self,text):
    self.results += [text]
def flush(self):
    pass
def print_(self):
    for x in self.results: print(x)
def return_(self):
    return self.results
def clear_(self):
    self.results = []

when passed as

O = OUTPUT() # Necessarily to remember results, but see below.

help = pydoc.Helper(O)

will store all results in the OUTPUT instance. Of course, beginning with O = OUTPUT() is not the best idea (see below). render_doc is not the central output point; output is. I wanted OUTPUT so I could keep large outputs from disappearing from the screen using something like Mark Lutz’ “More”. A different OUTPUT would allow you to write to files.

You could also add a “return” to the end of the class pydoc.Helper to return the information you want. Something like:

if self.output_: return self.output_

should work, or

if self.output_: return self.output.return_()

All of this is possible because pydoc is well-designed. It is hidden because the definition of help leaves out the input and output arguments.

Answered By: Clifford

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')
Answered By: Chris_Rands

Selected answer didn’t work for me, so I did a little more searching and found something that worked on Daniweb. Credit goes to vegaseat. https://www.daniweb.com/programming/software-development/threads/20774/starting-python/8#post1306519

# simplified version of sending help() output to a file
import sys
# save present stdout
out = sys.stdout
fname = "help_print7.txt"
# set stdout to file handle
sys.stdout = open(fname, "w")
# run your help code
# its console output goes to the file now
help("print")
sys.stdout.close()
# reset stdout
sys.stdout = out
Answered By: Matt

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
Answered By: N.Elsayed

To get a “clean” text output, just as the built-in help() would deliver, and suitable for exporting to a file or anything else, you can use the following:

>>> import pydoc
>>> pydoc.render_doc(len, renderer=pydoc.plaintext)
'Python Library Documentation: built-in function len in module builtinsnnlen(obj, /)n    Return the number of items in a container.n'
Answered By: flexatone

The cleanest way

Assuming help(os)

Step 1 – In Python Console

 import pydoc 
 pydoc.render_doc(os, renderer=pydoc.plaintext)` 
 
 #this will display a string containing help(os) output 

Step 2 – Copy string

Step 3 – On a Terminal

echo "copied string" | tee somefile.txt 

If you want to write Class information in a text file. Follow below steps

  1. Insert pdb hook somewhere in the Class and run file

    import pdb; pdb.set_trace()

  2. Perform step 1 to 3 stated above

Answered By: Samir Kape

Using the command line we can get the output directly and pipe it to whatever is useful.

python -m pydoc ./my_module_file.py

— the ./ is important, it tells pydoc to look at your local file and not attempt to import from somewhere else.

If you’re on the mac you can pipe the output to pbcopy and paste it into a documentation tool of your choice.

python -m pydoc ./my_module_file.py | pbcopy
Answered By: Garry Polley
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.