How to retrieve the output of subprocess.call() with Python?

Sometimes, we want to retrieve the output of subprocess.call() with Python.

In this article, we’ll look at how to retrieve the output of subprocess.call() with Python.

How to retrieve the output of subprocess.call() with Python?

To retrieve the output of subprocess.call() with Python, we call subprocess.check_output.

For instance, we write

import subprocess

print(subprocess.check_output(["ping", "-c", "1", "8.8.8.8"]))

to call subprocess.check_output with a list that has the command and command arguments.

And then we get the output from the returned string.

Conclusion

To retrieve the output of subprocess.call() with Python, we call subprocess.check_output.