Results 1 to 4 of 4

Thread: [Python] Not Everything from STDERR/STDOUT Being Redirected

  1. #1
    Join Date
    May 2008
    Beans
    1,029

    Question [Python] Not Everything from STDERR/STDOUT Being Redirected

    I've got a class that redirects stdout/stderr to itself:

    PHP Code:
    class OutputLog(wx.TextCtrl):
        
    def __init__(selfparentid=-1):
            
    wx.TextCtrl.__init__(selfparentidstyle=wx.TE_MULTILINE|wx.TE_READONLY)
            
    self.SetBackgroundColour(u'black')
            
    self.SetForegroundColour(u'white')
            
    self.stdout sys.stdout
            self
    .stderr sys.stderr
        
        def write
    (selfstring):
            
    self.AppendText(string)
        
        
    def ToggleOutput(selfevent=None):
            if (
    sys.stdout == self):
                
    sys.stdout self.stdout
                sys
    .stderr self.stderr
            
    else:
                
    sys.stdout self
                sys
    .stdout self 
    The problem is that not everything is redirected to it. It works when I use the "print" function, but not for system calls or errors.

    Here is an example:
    PHP Code:
    ...
        
    self.outputlog OutputLog(self)
        
    self.outputlog.ToggleOutput() # Set stdout/stderr to print to log
    ...
      
    def PrintSomething(selfevent=None):
        print 
    'Hello from Python'
        
    os.system('echo "Hello from system"')
        
    subprocess.Popen(['echo''Hello from subprocess'])
        print 
    hello # Force a NameError 
    "Hello from Python" is printed in the output log, but "Hello from system", "Hello from subprocess" and the error traceback are printed in the system terminal. Is there a way to redirect all of this as well?

    ----- EDIT -----

    I figured out how to manually do tracebacks, but it would be nice if all tracebacks were automatically sent to the OutputLog:

    PHP Code:
    ...
        try:
          print 
    hello
        except NameError
    :
          
    traceback.print_exc(file=sys.stdout# Can also use "file=self.outputlog" 
    ----- EDIT -----

    Okay, figured out how to get it from os and subprocess modules, although once again it is not automatic, I have to put the output into a variable then print it:

    PHP Code:
    ...
        
    os.popen('echo "Hello from system"').read()
        print 
    o
        s 
    subprocess.Popen(['echo''Hello from subprocess'], stdout=subprocess.PIPEstderr=subprocess.PIPE)
        print 
    s.stdout.read() 
    I'm not going to mark this thread as solved for a couple of days in case someone has a better answer. Is there a way to redirect the output of os.popen or subprocess.Popen without having to put it into a variable?
    Last edited by dodle; December 10th, 2011 at 02:38 AM.

  2. #2
    Join Date
    Aug 2007
    Location
    UK
    Beans
    427
    Distro
    Ubuntu UNR

    Re: [Python] Not Everything from STDERR/STDOUT Being Redirected

    If you want to globally specify the IO streams you can do so at launch time or by writing a simple launch script.
    Code:
    $ python 2>&1 >myfile
    Python 2.7.2 (default, Oct 24 2011, 11:20:40) 
    [GCC 4.5.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.system("echo hello")
    >>> 
    $ cat myfile
    hello
    0
    The 0 is the return value from os.system which Python prints by default in interactive mode.

  3. #3
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    978
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Python] Not Everything from STDERR/STDOUT Being Redirected

    Quote Originally Posted by dodle View Post
    PHP Code:
    ...
        
    os.popen('echo "Hello from system"').read()
        print 

    I'm not going to mark this thread as solved for a couple of days in case someone has a better answer. Is there a way to redirect the output of os.popen or subprocess.Popen without having to put it into a variable?
    Your almost there, what happens if you don't do the variable assignment?

    Code:
    print os.popen('echo "Hello from system"').read()
    Bodsda
    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


  4. #4
    Join Date
    May 2008
    Beans
    1,029

    Re: [Python] Not Everything from STDERR/STDOUT Being Redirected

    Quote Originally Posted by StephenF View Post
    If you want to globally specify the IO streams you can do so at launch time or by writing a simple launch script.
    That's sounds pretty good, I'm going to look into that.

    @Bodsda
    I knew that I could use the print function without first putting the output into a variable, but I wanted it to go to stdout/stderr without having to call the print function at all.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •