I am using myFile.write("...")
often and sometimes I'd like to see the output in the console rather than re-opening the updated file. I am using IDLE.
So, I'd like to know, instead of going and replacing myFile.write()
with print()
everywhere, can I replace the filename (output.txt
) with something like STDIO when setting the myFile variable?
myFile = open("output.txt", "w")
You can use the sys
module...
import sys
myFile=sys.stdout
myFile.write("Hello!\n")
sys.stderr
is also available.
Here you go: sys.stdout. Just use it like any other file handle. E.g.
print >> sys.stderr, "Hi! I'm an error!"
print ("Hi! I'm an error!",file=sys.stderr)
mgilson 2012-04-04 02:16