-
Notifications
You must be signed in to change notification settings - Fork 0
subprocess usage
ryanm292002 edited this page Apr 29, 2024
·
1 revision
To use the subprocess module, you must import it into your Python script:
import subprocess
Here are some common uses of the subprocess module:
Running an External Command The following runs a basic echo which prints out "Hello World" in result section:
subprocess.run(['echo', 'Hello, World!'])
To capture the output of a command, use the capture_output=True argument:
result = subprocess.run(['ls', '-l'], capture_output=True, text=True) print(result.stdout)
To handle errors in a subprocess, check the returncode:
result = subprocess.run(['false']) # 'false's if result.returncode != 0: print("Error: Command failed with status", result.returncode)