Calling an external command from Python
This example will show you how to call an external command from Unix shell or Windows command prompt in a python script.
To call a external command from Unix shell or Windows command prompt in Python, we can use the built-in subprocess.run()
method which is available in subprocess module.
import subprocess
subprocess.run(["ls", "-l"])
For old versions, we can use the call()
method.
import subprocess
subprocess.call(["ls", "-l"])
The subprocess module allows us to spawn new processes and connect to their input/output/error pipes, and obtain their return codes.