Created
June 17, 2017 00:31
-
-
Save mostaphaRoudsari/1d961fcd5e8db30032e71b34856511a6 to your computer and use it in GitHub Desktop.
Redirect print to a variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Redirect print to a variable. | |
""" | |
from cStringIO import StringIO | |
import sys | |
originalStdout = sys.stdout | |
sys.stdout = newStdout = StringIO() | |
# code ... | |
print 'covfefe' | |
print 'covfefe' | |
sys.stdout = originalStdout | |
# read the values from StringIO | |
mystdout.seek(0) | |
text = mystdout.read() | |
print text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment