我正在使用以下构造,尽管您可能要避免使用shell=True
。这将为您提供任何命令的输出和错误消息,以及错误代码:
process = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# wait for the process to terminate
out, err = process.communicate()
errcode = process.returncode