@contextmanager
def capture_stdout():
 org = sys.stdout
 sys.stdout = capture = StringIO()
 if six.PY2:
  sys.stdout.encoding = 'utf-8'
 try:
  (yield sys.stdout)
 finally:
  sys.stdout = org
  print(capture.getvalue())

@ contextmanager def capture_stdout ( ) : org = sys . stdout sys . stdout = capture = StringIO ( ) if six . PY2 : sys . stdout . encoding = 'utf-8' try : ( yield sys . stdout ) finally : sys . stdout = org print ( capture . getvalue ( ) )
save stdout in a stringio .