How to redirect stderr in Python?

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

    Actually, if you're using linux/mac os, you can just use file redirect to do that. For example, if you're going to run "a.py" and record all the messages it will generate into file "a.out", it would just be

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

    The first part redirects stderr to stdout, and the second redirects that to a file called a.out.

    For a longer list of redirection operators in Linux/Unix, see https://askubuntu.com/questions/420981/how-do-i-save-terminal-output-to-a-file

提交回复
热议问题