redirecting stderr back..

Discussion in 'Python' started by rh0dium, Jan 9, 2009.

  1. rh0dium

    rh0dium Guest

    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
     
    rh0dium, Jan 9, 2009
    #1
    1. Advertising

  2. rh0dium

    Robert Kern Guest

    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-bin/hgwebdir.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
     
    Robert Kern, Jan 9, 2009
    #2
    1. Advertising

  3. rh0dium

    rh0dium Guest

    On Jan 9, 3:52 pm, Christian Heimes <> 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..
     
    rh0dium, Jan 9, 2009
    #3
  4. rh0dium

    rh0dium Guest

    On Jan 9, 3:42 pm, Robert Kern <> 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-bin/hgwebdir.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.
     
    rh0dium, Jan 9, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Landenburer

    redirecting stderr and stdout

    Jon Landenburer, May 12, 2004, in forum: Perl
    Replies:
    1
    Views:
    11,217
    Joe Smith
    May 13, 2004
  2. Jan Knop
    Replies:
    4
    Views:
    1,051
    Jan knob
    Nov 24, 2003
  3. flupke

    wxPython & redirecting stderr

    flupke, Aug 10, 2004, in forum: Python
    Replies:
    2
    Views:
    337
    flupke
    Aug 11, 2004
  4. Michele Simionato

    redirecting stderr

    Michele Simionato, Nov 25, 2004, in forum: Python
    Replies:
    2
    Views:
    300
    Peter Otten
    Nov 25, 2004
  5. rh0dium

    redirecting stderr back..

    rh0dium, Jan 9, 2009, in forum: Python
    Replies:
    2
    Views:
    300
    Steve Holden
    Jan 13, 2009
Loading...

Share This Page