How to execute a program or call a system command with Python?

Sometimes, we want to execute a program or call a system command with Python.

In this article, we’ll look at how to execute a program or call a system command with Python.

How to execute a program or call a system command with Python?

To execute a program or call a system command with Python, we can use the subprocess.run method.

For instance, we write:

import subprocess
subprocess.run(["ls", "-l"])

We pass in an array with the command as the first entry.

And the options are in the subsequent entries.

Now we should see the listings of the content of the current working directory displayed.

Conclusion

To execute a program or call a system command with Python, we can use the subprocess.run method.