Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
capturing both stderr and std to a single list of strings.
from cStringIO import StringIO
class Capturing(list):
def __enter__(self):
self._stdout = sys.stdout
self._stderr = sys.stderr
sys.stdout = self._stringio = StringIO()
sys.stderr = self._stringio
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout
sys.stderr = self._stderr
@claytantor

This comment has been minimized.

Copy link
Owner Author

@claytantor claytantor commented Jun 19, 2017

# creates a list
with Capturing() as output:
    print "foo" #can also send to stderr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment