obspy.core.util.misc.CatchOutput

CatchOutput(*args, **kwds)[source]

A context manager that catches stdout/stderr for its scope.

Always use with “with” statement. Does nothing otherwise.

Roughly based on: http://stackoverflow.com/a/17954769

This variant does not leak file descriptors.

>>> with CatchOutput() as out:  
...    os.system('echo "mystdout"')
...    os.system('echo "mystderr" >&2')
>>> print out.stdout  
mystdout
>>> print out.stderr  
mystderr

This Page