How to execute a program or call a system command?
Explanation
Here we have to execute a program or call a system command
Here we will use the subprocess
module in the standard library
import subprocess
subprocess.run(["ls", "-l"])
Now here we have the advantage of subprocess.run
over os.system
. And that advantage is that it is more flexible. Which is useful in many ways.
Also instead of using subprocess
recommends
os.system
for the documentation
And there is also the subprocess
the module always provides some more powerful facilities for spawning new processes. And to retrieve their results. Using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess
documentation for some helpful recipes.
On Python 3.4 and earlier, use subprocess.call
instead of .run
:
subprocess.call(["ls", "-l"])
In this way, we can execute a program or call a system command
Also read, Write a function that takes a list of tuples as its argument