8

If I execute a function at the Python or Ipython command prompt, such as 'help(dir)':

>>> help(dir)
Help on built-in function dir in module __builtin__:

dir(...)
    dir([object]) -> list of strings

    If called without an argument, return the names in the current scope.

I'd like to capture the resulting output in a file or variable, but

>>> x = help(dir)        
>>> help(dir) >file.txt   
>>> help(dir) >>file.txt

do not work. I see a related question (Redirect an output command to a variable or file?) though it is awfully complicated, would be difficult to remember on the fly, and it unclear whether it even applies here.

In the bash shell, output can be redirected with > or 2>. Seems like it should be easy to do something similar in the Python or Ipython shell.

2 Answers 2

12

Even better than the methods above, (both of which will work), as much easier to remember and no imports needed, is the iPython magic %%capture:

In [38]: %%capture myout
   ....: help(dir)
   ....:

In [39]: print myout.stdout
Help on built-in function dir in module __builtin__:

dir(...)
    dir([object]) -> list of strings

    If called without an argument, return the names in the current scope.
    Else, return an alphabetized list of names comprising (some of) the attributes 
    of the given object, and of attributes reachable from it.
    If the object supplies a method named __dir__, it will be used; otherwise
    the default dir() logic is used and returns:
      for a module object: the module's attributes.
      for a class object:  its attributes, and recursively the attributes
        of its bases.
      for any other object: its attributes, its class's attributes, and
        recursively the attributes of its class's base classes.
Sign up to request clarification or add additional context in comments.

1 Comment

This was the easiest solution for me. Once you're done with your script, just do a simple with open('filename.txt', 'a+') as outfile: outfile.write(myout) and you're done.
6

Use IPython capture_output function

In [21]: from IPython.utils import io

In [22]: with io.capture_output() as captured:
   ....:   help(dir)
   ....:   

In [23]: print captured.stdout
Help on built-in function dir in module __builtin__:

dir(...)
    dir([object]) -> list of strings

    If called without an argument, return the names in the current scope.

Update

In case above solution does not work, you can use ipython command output capture feature. E.g.:

In [6]: output = !python -c 'help(dir)' | cat

In [7]: output
Out[7]: 
['Help on built-in function dir in module __builtin__:',
 '',
 'dir(...)',
 '    dir([object]) -> list of strings',

7 Comments

Thank you, Dmitry - but I get different results when I type exactly as above. After
I believe your comment is cropped. What did you mean you get different results? Btw, I used ipython shell.
Thank you, Dmitry - but I get different results when I type exactly as above. I import from IPython.utils import io. Then after In [22], the output of help(dir) is displayed, and after In [23], nothing is displayed.
I get the same results in the python and ipython shell.
It does! Thank you very much Dmitry! I see I can print the result more nicely to screen with print '\n'.join(output) - or saved to a file, etc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.