Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception handling with python subprocess.run

I am new to Python. I need to catch all the error / exceptions from subprocess.run.

Currently, I have a Python file which has subprocess.run(shell script) [ runs a shell script ]

I need to catch all the exceptions from this process,

I tried except 'Exception as e: print(e)' but couldn't see all the errors.

like image 276
Sathya Avatar asked Oct 20 '25 03:10

Sathya


1 Answers

According to doc subprocess.check_output()_doc, you can handle the exception as follow:

try:
    subprocess.run(...)
except subprocess.CalledProcessError as e:
    print e.output
like image 132
Mert Köklü Avatar answered Oct 21 '25 16:10

Mert Köklü