Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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