I am looking to write console based programs in python that can execute functions to perform generic tasks, pretty generic. Is it possible to capture everything written to the c
You can do this by setting sys.stdout
to be a file of your choice
import sys
sys.stdout = open('out.dat', 'w')
print "Hello"
sys.stdout.close()
Will not display any output but will create a file called out.dat
with the printed text.
Note that this doesn't need to be an actual file but could be a StringIO instance, which you can just use the getvalue
method of to access everything that has been printed previously.