草庐IT

CalledProcessError

全部标签

python - 当子进程引发 CalledProcessError 异常时检查命令的返回码

我想在python(3)脚本中捕获shell命令的stdout流,同时能够检查shell命令的返回码是否返回一个错误(也就是说,如果它的返回码不是0)。subprocess.check_output似乎是执行此操作的合适方法。来自subprocess的手册页:check_output(*popenargs,**kwargs)Runcommandwithargumentsandreturnitsoutputasabytestring.Iftheexitcodewasnon-zeroitraisesaCalledProcessError.TheCalledProcessErrorobjec