[Tutor] stderr
Corran Webster
cwebster@math.tamu.edu
Tue, 13 Apr 1999 10:26:31 -0500 (CDT)
On 13 Apr, Arne Mueller wrote:
> Hi All,
>
> is there an easy way like in C or perl to use 'print' to write to stderr
> instead stdout?
>
> python> print sys.stderr 'error'
>
> that doesn't work :-(
>
> assigning sys.stdout to sys.stderr is not a good solution. I dont want
> to reassign stderr/stdout everytime I switch between error messages and
> data output.
>
> python> sys.stdout = sys.stderr
> python> print 'error'
>
> ... work but is not usefull
sys.stderr is a file object, so
sys.stderr.write("Oops!\n")
is equivalent to printing "Oops" to stderr after switching. If you
need to vary the contents of the message, you'll probably find the %
string formatting commands useful:
sys.stderr.write("Oops! Error: %d, %s\n" % (errnum, message))
Regards,
Corran