redirecting stderr

Michele Simionato michele.simionato at gmail.com
Thu Nov 25 07:10:29 EST 2004


Maybe it is something obvious, but what is going on with this code?

import sys
myerr = file("myerr.txt", "w")
sys.stderr = myerr
try:
    raise Exception, "some error"
finally:
    myerr.close()
    sys.stderr = sys.__stderr__

I would expect the error message to be written into "myerr.txt", instead
it is displayed on the console, on regular stderr (?) and "myerr.txt" is
empty. I guess I misunderstood something ...

                  Michele Simionato



More information about the Python-list mailing list