Asfbar
Asfbar

Reputation: 279

Get exit status of last command in Python script

How can I get the exist status (if command succeed or not) of any line in Python?

For example in bash, $? will tell me the last exist status of any command. I need it to know if my connection to FTP server was successful or not.

Upvotes: 1

Views: 7352

Answers (2)

Jorge
Jorge

Reputation: 66

Have you tried using a try/catch? If there was an error while executing the command, an exception will be raised. You can retrieve it with the sys module.

Example code:

import sys 

try:
    run_command()
except:
    e = sys.exc_info()[0]
    print(e)

Upvotes: 1

user3696412
user3696412

Reputation: 1419

If it is a function you call, it should have a retrun code you can collect like

retVal = doSomething()

Then you can check what happened.

Upvotes: 0

Related Questions