May-08-2017, 09:49 PM
You could also redirect the output to a stringio object...
>>> import io >>> text = io.StringIO() >>> print("just some stuff", file=text) >>> print("more things", file=text) >>> text.seek(0) 0 >>> text.readlines() ['just some stuff\n', 'more things\n'] >>> text.getvalue() 'just some stuff\nmore things\n'