Running Commands with subprocess
Execute external programs.
What Is subprocess?
The subprocess module lets your Python program run external commands and other programs, just like typing them in a terminal.
import subprocess
result = subprocess.run(['echo', 'hello'])
print('return code:', result.returncode)subprocess.run Basics
subprocess.run() is the recommended entry point. You pass the command as a list of arguments: the program name first, then each argument separately.
import subprocess
result = subprocess.run(['echo', 'one', 'two'])
print('done with code', result.returncode)All lessons in this course
- Running Commands with subprocess
- Capturing Output
- Environment and os Module
- shutil for File Operations