Why Does a Python Variable Keep Getting Assigned to 0 When the Output of the Bash Command Is a Different Number?

Problem scenario
In a Python script a variable that receives the output of a Bash command. When you run the Bash command on Linux or in a script, it works fine. But in the Python script it keeps being assigned to 0. What is wrong?

Solution

subprocess.call("bash command", shell=True) is NOT the same as subprocess.check_output("bash command", shell=True).

The subprocess.call will return a 0 if the Bash command was successful. The subprocess.check_output command will return the output of the Bash command itself.

For more information, see this posting https://stackoverflow.com/questions/25333537/performance-of-subprocess-check-output-vs-subprocess-call.

Leave a comment

Your email address will not be published. Required fields are marked *