捕获 python3 中子进程的所有输出

Capturing all output from subprocess in python3

我想将所有输出捕获到子进程打印出的变量中。这是我的代码:

#!/usr/bin/env python3

import subprocess # Subprocess management
import sys # System-specific parameters and functions

try:
    args = ["svn", "info", "/directory/that/does/not/exist"]
    output = subprocess.check_output(args).decode("utf-8")
except subprocess.CalledProcessError as e:
    error = "CalledProcessError: %s" % str(e)
except:
    error = "except: %s" % str(sys.exc_info()[1])
else:
    pass

此脚本仍将其打印到终端中:

svn: E155007: '/directory/that/does/not/exist' is not a working copy

如何将其捕获到变量中?

check_output 仅捕获 stdout 而不是 stderr(根据 https://docs.python.org/3.6/library/subprocess.html#subprocess.check_output

为了捕获 stderr,您应该使用

>>> subprocess.check_output(
...     "ls non_existent_file; exit 0",
...     stderr=subprocess.STDOUT, ...)

顺便问一下,我建议先阅读文档。