How to redirect stderr in Python?

前端 未结 10 923
暖寄归人
暖寄归人 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 07:13

    I have a piece of software I wrote for work that captures stderr to a file like so:

    import sys
    sys.stderr = open('C:\\err.txt', 'w')
    

    so it's definitely possible.

    I believe your problem is that you are creating two instances of writer.

    Maybe something more like:

    import sys
    
    class writer(object):
        log = []
    
        def write(self, data):
            self.log.append(data)
    
    logger = writer()
    sys.stdout = logger
    sys.stderr = logger
    
    0 讨论(0)
  • 2020-11-27 07:13

    I found this approach to redirecting stderr particularly helpful. Essentially, it is necessary to understand if your output is stdout or stderr. The difference? Stdout is any output posted by a shell command (think an 'ls' list) while sterr is any error output.

    It may be that you want to take a shell commands output and redirect it to a log file only if it is normal output. Using ls as an example here, with an all files flag:

    # Imports
    import sys
    import subprocess
    
    # Open file
    log = open("output.txt", "w+")
    
    # Declare command
    cmd = 'ls -a'
    # Run shell command piping to stdout
    result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True)
    # Assuming utf-8 encoding
    txt = result.stdout.decode('utf-8')
    # Write and close file
    log.write(txt)
    log.close()
    

    If you wanted to make this an error log, you could do the same with stderr. It's exactly the same code as stdout with stderr in its place. This pipes an error messages that get sent to the console to the log. Doing so actually keeps it from flooding your terminal window as well!

    Saw this was a post from a while ago, but figured this could save someone some time :)

    0 讨论(0)
  • 2020-11-27 07:23

    To route the output and errors from Windows, you can use the following code outside of your Python file:

    python a.py 1> a.out 2>&1
    

    Source: https://support.microsoft.com/en-us/help/110930/redirecting-error-messages-from-command-prompt-stderr-stdout

    0 讨论(0)
  • 2020-11-27 07:24

    Python will not execute your code if there is an error. But you can import your script in another script an catch exceptions. Example:

    Script.py

    print 'something#
    

    FinalScript.py

    from importlib.machinery import SourceFileLoader
    
    try:
        SourceFileLoader("main", "<SCRIPT PATH>").load_module()
    except Exception as e:
        # Handle the exception here
    
    0 讨论(0)
提交回复
热议问题