I would like to have my Python script run a Linux shell command and store the output in a variable, without the command's output being shown to the user. I have tried this with os.system, subprocess.check_output, subprocess.run, subprocess.Popen, and os.popen with no luck.
My current method is running os.system("ls -l &> /tmp/test_file") so the command stdout and stderr are piped to /tmp/test_file, and then I have my Python code read the file into a variable and then delete it.
Is there a better way of doing this so that I can have the command output sent directly into the variable without having to create and delete a file, but keep it hidden from the user?
You can use the subprocess.run function.
One update as @Ashley Kleynhans say
"The results of stdout and stderr are bytes objects so you will need to decode them if you want to handle them as strings"
For this, you do not need to decode stdout or stderr because in the run method you can pass one more argument to get the return data as a string, which is text=True
"If used it must be a byte sequence, or a string if encoding or errors is specified or text is true" - from Documentation
from subprocess import run
data = run("ANY COMMAND HERE", capture_output=True, shell=True, text=True)
print(data.stdout) # If you want you can save it to a variable
print(data.stderr) # ^^
The solution by @codester_09 is technically correct but only answers half the question and does not show how to assign the output to a variable.
The results of stdout and stderr are bytes objects so you will need to decode them if you want to handle them as strings, for example:
from subprocess import run
cmd = 'ls -l'
data = run(cmd, capture_output=True, shell=True)
output = data.stdout.splitlines()
errors = data.stderr.splitlines()
combined = output + errors
# Do whatever you want with each line of stdout
for line in output:
line = line.decode('utf-8')
# Do whatever you want with each line of stderr
for line in errors:
line = line.decode('utf-8')
# Do whatever you want with each line of stdout and stderr combined
for line in combined:
line = line.decode('utf-8')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With