these are my python codes:
import subprocess
subprocess.check_output("ls",shell=True,stderr=subprocess.STDOUT)
import subprocess
subprocess.check_output("yum",shell=True,stderr=subprocess.STDOUT)
the first work well, but the second return:
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
why this happen? is that because ls is the original shell command but yum is the new package? how to solve this problem?
The word
check_
in the name means that if the command (the shell in this case that returns the exit status of the last command (yum
in this case)) returns non-zero status then it raisesCalledProcessError
exception. It is by design. If the command that you want to run may return non-zero status on success then either catch this exception or don't usecheck_
methods. You could usesubprocess.call
in your case because you are ignoring the captured output, e.g.:You don't need
shell=True
to run the commands from your question.The command
yum
that you launch was executed properly. It returns a non zero status which means that an error occured during the processing of the command. You probably want to add some argument to youryum
command to fix that.Your code could show this error this way: