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, ...)
顺便说一句,我建议在询问之前先阅读文档。这很有效,谢谢。我确实检查了手册和几篇关于这个的帖子,但是我仍然没有让它自己工作。