redirecting stderr back..

R

rh0dium

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)

cmds = os.popen("ls alaksjdf")

<Now what do I need to do here to make this come back?>

sys.stderr.write("Foobar\n")
Foobar

Any ideas? Thanks
 
Ð

Дамјан ГеоргиевÑки

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)
 
S

Steve Holden

Дамјан ГеоргиевÑки said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top