redirecting stderr back..

Steve Holden steve at holdenweb.com
Mon Jan 12 21:14:28 EST 2009


Дамјан Георгиевски wrote:
> 
>> Hi All,
>>
>> Can someone tell me how to redirect stderr back to the console once
>> you've moved it?
>>
>> import os,sys
>> se = os.open("/tmp/mod.log", os.O_WRONLY|os.O_APPEND|os.O_CREAT)
>> sys.stderr.write("Foobar\n")
>> Foobar
>> os.dup2(se, 2)
> 
> why not os.dup2(2, 10) and then later os.dup2(10, 2)
> 
I'd have thought it would be necessary to do something like that in the
general case to ensure that the original standard error remained open.

os.dup2 will close the original file unless it's still open on another
descriptor.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list