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

。_饼干妹妹 提交于 2019-11-27 22:55:08
Michael0x2a

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')
Aaron Altman

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

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...

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')

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:\myFolder\memberName.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:\myFolder\UltimateListCtrl.txt

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

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.

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

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!