Categories
Python Answers

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

Spread the love

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.

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 *