Q3
How do you capture standard output and check return codes using `subprocess.run()`?
💬Answer
Pass stdout=subprocess.PIPE and stderr=subprocess.PIPE. You can then check the exit status using returncode.
import subprocess
result = subprocess.run("echo 'hello'", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("Exit Status:", result.returncode)
print("Output:", result.stdout.decode().strip())
Related Python Questions
View All PythonQuestions →
Created by
Apurv Gujjar
DevOps & Cloud Engineer
Specialized in:DevOpsAWSGCPKubernetesTerraformDocker
View Portfolio