Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > redirecting stderr back..

Reply
Thread Tools

redirecting stderr back..

 
 
rh0dium
Guest
Posts: n/a
 
      01-09-2009
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



 
Reply With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      01-09-2009
Christian Heimes wrote:
> rh0dium schrieb:
>> Hi All,
>>
>> Can someone tell me how to redirect stderr back to the console once
>> you've moved it?

>
> sys.stderr = sys.__stderr__
>
> or better:
>
> orig_stderr = sys.stderr
> try:
> sys.stderr = open(...)
> ...
> finally:
> sys.stderr = orig_stderr


He's not reassigning the sys.stderr object; he's fiddling with the underlying
file descriptor.

Steven, look at my code here for a mostly complete solution:

http://www.enthought.com/~rkern/cgi-...dir.cgi/redir/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
 
 
 
rh0dium
Guest
Posts: n/a
 
      01-09-2009
On Jan 9, 3:52*pm, Christian Heimes <(E-Mail Removed)> wrote:
> Robert Kern schrieb:
>
>
>
> > Christian Heimes wrote:
> >> rh0dium schrieb:
> >>> Hi All,

>
> >>> Can someone tell me how to redirect stderr back to the console once
> >>> you've moved it?

>
> >> sys.stderr = sys.__stderr__

>
> >> or better:

>
> >> orig_stderr = sys.stderr
> >> try:
> >> * * sys.stderr = open(...)
> >> * * ...
> >> finally:
> >> * * sys.stderr = orig_stderr

>
> > He's not reassigning the sys.stderr object; he's fiddling with the
> > underlying file descriptor.

>
> I know that. I wanted to show him a better way than messing around with
> file descriptors. In Python one doesn't have to (and should not) alter
> the file descriptor of a standard stream. Python always uses sys.stderr
> to write to the standard error stream.
>
> Christian


Christian - it's not python that's the problem. Unfortunately the
stuff I'm calling (outside of python) won't play nicely unless I mess
with the FD's..


 
Reply With Quote
 
rh0dium
Guest
Posts: n/a
 
      01-09-2009
On Jan 9, 3:42*pm, Robert Kern <(E-Mail Removed)> wrote:
> Christian Heimes wrote:
> > rh0dium schrieb:
> >> Hi All,

>
> >> Can someone tell me how to redirect stderr back to the console once
> >> you've moved it?

>
> > sys.stderr = sys.__stderr__

>
> > or better:

>
> > orig_stderr = sys.stderr
> > try:
> > * * sys.stderr = open(...)
> > * * ...
> > finally:
> > * * sys.stderr = orig_stderr

>
> He's not reassigning the sys.stderr object; he's fiddling with the underlying
> file descriptor.
>
> Steven, look at my code here for a mostly complete solution:
>
> * *http://www.enthought.com/~rkern/cgi-...dir.cgi/redir/
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless enigma
> * that is made terrible by our own mad attempt to interpret it as though it had
> * an underlying truth."
> * *-- Umberto Eco


Interesting stuff - but the documentation is a bit lacking - but I
think I can figure it out.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirecting stdin, stdout, and stderr to a window Michael McGarry Python 1 12-16-2004 11:29 PM
redirecting stderr Michele Simionato Python 2 11-25-2004 01:25 PM
wxPython & redirecting stderr flupke Python 2 08-10-2004 11:56 PM
redirecting stderr and stdout Jon Landenburer Perl 1 05-13-2004 07:38 AM
Redirecting Python stdout ,stderr and stdin Jan Knop Python 4 11-24-2003 10:14 AM



Advertisments