1

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.

3
  • No, there's no such concept. All you can get is the return value from a function. How are you connecting to the FTP server? Are you using ftplib? Also, add your code. Commented Jun 16, 2015 at 8:04
  • 3
    Python is not Bash. Calling functions or methods of classes does not cause an exit status to be set. It may return a value or throw an exception. But there is nothing like $? in Bash. If you want to use ftplib, read the documentation. There are various exceptions described there. Commented Jun 16, 2015 at 8:05
  • Thank you for the explanation. and yes, i use ftplib Commented Jun 16, 2015 at 8:06

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.