class Capturing(list):

    def __enter__(self):
        self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self

def __exit__(self, *args):
    self.extend(self._stringio.getvalue().splitlines())
del self._stringio
sys.stdout = self._stdout
How to capture stdout output from a Python function call?