这是我的python代码:
import subprocess subprocess.check_output("ls",shell=True,stderr=subprocess.STDOUT) import subprocess subprocess.check_output("yum",shell=True,stderr=subprocess.STDOUT)
第一个.check_output()工作良好,但是第二个返回此:
.check_output()
Traceback (most recent call last): File "/usr/lib/x86_64-linux-gnu/gedit/plugins/pythonconsole/console.py", line 378, in __run r = eval(command, self.namespace, self.namespace) File "<string>", line 1, in <module> File "/usr/lib/python3.4/subprocess.py", line 616, in check_output raise CalledProcessError(retcode, process.args, output=output) subprocess.CalledProcessError: Command 'yum' returned non-zero exit status 1
为什么会这样?是因为ls原始的shell命令而是yum新的软件包吗?我怎么解决这个问题?
ls
yum
yum您启动的命令已正确执行。它返回非零状态,这意味着在处理命令期间发生了错误。您可能想在yum命令中添加一些参数来解决该问题。
您的代码可能以这种方式显示此错误:
import subprocess try: subprocess.check_output("dir /f",shell=True,stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))