Search code examples
pythonpython-3.xsubprocesscapture

Python - Run shell command, get stdout and stderr as a variable, but hide from user?


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?


Solution

  • 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) # ^^