direct standard error to same file and standard output?
I have something like
f=open('out.txt','w')
print >>f, 'action=%r'%action
in one of my Python pro开发者_开发百科grams. Is it possible to direct standard error to the same file as standard output?
Thanks!
You could do this...
import sys
f = open("error.log", "w")
sys.stderr = f
print "raising exception"
raise Exception, "find this in error.log"
Or to answer your question more directly,
import sys
f = open("logall.txt", "w")
sys.stderr = f
sys.stdout = f
print "find this in logall.txt"
raise Exception, "find this in logall.txt"
Although I don't necessarily recommend the latter.
You might just say this:
sys.stderr = sys.stdout
But I'd wager if your script will ever be maintained by someone else, you might cause some confusion. Rather than doing this inside the script, would it be an acceptable solution to do it from the calling shell?
myscript 2>&1
精彩评论