Error Handling for Subprocess at Python

This sample is for error handling for subprocess.Popen. It confirms whether the execution file is existing. If the execution file is also not in the path, the error message is shown.

import subprocess

res = subprocess.Popen(
    "application",  #  <- Execution file
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    shell=True
).communicate()

if len(res[1]) == 0:
    print("ok: Application is existing.")
else:
    print("Error: Application is not found.")

 Share!