I would like to check the result of a Bash command in a Python script.
I'm using subprocess.check_output()
.
Before that, I checked manually the result of two commands in a shell:
user@something:~$ hostname
> something
user@something:~$ command -v apt
> /usr/bin/apt
It works as expected. Now I'm trying to run the subprocess.check_output()
function in the Python interpreter:
>>> import subprocess
>>> subprocess.check_output(['hostname'], shell=True, stderr=subprocess.STDOUT)
b'something\n'
>>> subprocess.check_output(['command', '-v', 'apt'], shell=True, stderr=subprocess.STDOUT)
b''
As you can see, the first command is working as expected, but not the second (as it returns an empty string). Why is that?
EDIT
I already tried removing shell=True
, it returns an error:
>>> subprocess.check_output(['command', '-v', 'apt'], stderr=subprocess.STDOUT)
Traceback (most recent call last):
[...]
FileNotFoundError: [Errno 2] No such file or directory: 'command'