How to redirect stderr in Python?

前端 未结 10 963
暖寄归人
暖寄归人 2020-11-27 06:19

I would like to log all the output of a Python script. I tried:

import sys

log = []

class writer(object):
    def write(self, data):
        log.append(dat         


        
10条回答
  •  执念已碎
    2020-11-27 06:57

    I can't think of an easy way. The python process's standard error is living on a lower level than a python file object (C vs. python).

    You could wrap the python script in a second python script and use subprocess.Popen. It's also possible you could pull some magic like this in a single script:

    import os
    import subprocess
    import sys
    
    cat = subprocess.Popen("/bin/cat", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    os.close(sys.stderr.fileno())
    os.dup2(cat.stdin.fileno(), sys.stderr.fileno())
    

    And then use select.poll() to check cat.stdout regularly to find output.

    Yes, that seems to work.

    The problem I foresee is that most of the time, something printed to stderr by python indicates it's about to exit. The more usual way to handle this would be via exceptions.

    ---------Edit

    Somehow I missed the os.pipe() function.

    import os, sys
    r, w = os.pipe()
    os.close(sys.stderr.fileno())
    os.dup2(w, sys.stderr.fileno())
    

    Then read from r

提交回复
热议问题