There\'re lots of print
function (python 2.7
) in my program. Is there any way I can add a few lines then all the output can be redirected to
Do this in your method:
import sys
sys.stdout = sys.stderr
In python 2.7 you can do:
import sys
print >> sys.stderr, "To stderr."
Or you can import the behavior from 3.x:
from __future__ import print_function
import sys
print('To stderr.', file=sys.stderr)
Redefinition of print
is feature of Python 3+. However, you can change sys.stdout
to std.stderr
.
See: another question