redirect stdout to variable python

Solutions on MaxInterview for redirect stdout to variable python by the best coders in the world

showing results for - "redirect stdout to variable python"
Pat
22 Apr 2018
1from cStringIO import StringIO # Python3 use: from io import StringIO
2import sys
3
4old_stdout = sys.stdout
5sys.stdout = mystdout = StringIO()
6
7# blah blah lots of code ...
8
9sys.stdout = old_stdout
10
11# examine mystdout.getvalue()