For future visitors: Python 3.4 contextlib provides for this directly (see Python contextlib help) via the redirect_stdout
context manager:
from contextlib import redirect_stdout
import io
f = io.StringIO()
with redirect_stdout(f):
help(pow)
s = f.getvalue()
More Related Contents:
- Redirect stdout to a file in Python?
- Why should we NOT use sys.setdefaultencoding(“utf-8”) in a py script?
- multiple prints on the same line in Python
- Can I redirect the stdout into some sort of string buffer?
- Where is Python’s sys.path initialized from?
- How to capture stdout output from a Python function call?
- logger configuration to log to file and print to stdout
- Silence the stdout of a function in Python without trashing sys.stdout and restoring each function call
- Displaying subprocess output to stdout and redirecting it
- Usage of sys.stdout.flush() method
- Redirecting stdout and stderr to a PyQt4 QTextEdit from a secondary thread
- Python: How to get stdout after running os.system? [duplicate]
- How to disable logging on the standard error stream?
- subprocess readline hangs waiting for EOF
- How to redirect stdout and stderr to logger in Python
- Python 2.x – Write binary output to stdout?
- Print to the same line and not a new line?
- Suppress stdout / stderr print from Python functions
- Running powershell script within python script, how to make python print the powershell output while it is running
- Get output from a Paramiko SSH exec_command continuously
- Import from sibling directory
- How to redirect stdout to a Tkinter Text widget
- Set LD_LIBRARY_PATH before importing in python
- Integer File Descriptor “0” in open()
- How can I print multiple things on the same line, one at a time?
- remove last STDOUT line in Python
- “subprocess.Popen” – checking for success and errors
- How can I import files in Python using sys.path.append?
- How can I capture the stdout output of a child process?
- How to make print() output UTF-8 in Python 3.0?