Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在python3中捕获子进程的所有输出_Python 3.x_Svn_Terminal_Subprocess_Capture - Fatal编程技术网

Python 3.x 在python3中捕获子进程的所有输出

Python 3.x 在python3中捕获子进程的所有输出,python-3.x,svn,terminal,subprocess,capture,Python 3.x,Svn,Terminal,Subprocess,Capture,我想将所有输出捕获到子流程输出的变量中。这是我的密码: #!/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_outpu

我想将所有输出捕获到子流程输出的变量中。这是我的密码:

#!/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”不是工作副本


如何将其捕获到变量中?

检查\u输出仅捕获标准输出,而不捕获标准输出(根据)

为了捕获stderr,您应该使用

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

顺便说一句,我建议在询问之前先阅读文档。

这很有效,谢谢。我确实检查了手册和几篇关于这个的帖子,但是我仍然没有让它自己工作。