Python3 subprocess.call exception handling

Hi experts,

what is the best way to handle exception in below python3 subprocess scenario please?

I execute below line in my python script.

subprocess.call(['aws', 'ec2', 'start-instances', '--instance-ids', instance , '--output', 'table'])

Open in new window


could you suggest a best way to handle exception please? try/except?

Thanks in advance
enthuguyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Perhaps something like this.

from subprocess import Popen, PIPE

p = Popen(['aws', 'ec2', 'start-instances', '--instance-ids', instance , '--output', 'table'], stdout=PIPE)
output = p.communicate()[0]
if p.returncode != 0: 
   print("Subprocess failed %d %s" % (p.returncode, output))

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
enthuguyAuthor Commented:
thanks a lot Norie. Hope this is in python3. could you pls help me understand difference between Popen and subprocess.call

Will try and update you
enthuguyAuthor Commented:
Thanks a lot

understood from this
https://docs.python.org/3/library/subprocess.html
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
python3

From novice to tech pro — start learning today.