How to Get Exit Status in Python

Python is an object-oriented, open-source programming language used to create complex, dynamic, enterprise-wide applications. When programming in Python, you may need to figure out if a program successfully ran or if it encountered an error. In Python, you can get the exit status for any program of process using the "errno" module, which returns standard errno system codes, like interrupted system call, permission denied, invalid argument or bad address.

Instructions

    • 1

      Open your Python program in your Python editor.

    • 2

      Import the "errno" module by typing:

      import errno, sys

    • 3

      Capture the exit status in your program using the "sys.exit" function and returning an errno code. For example, type:

      if program_works ( ):
      run_program ( )
      else:
      sys.exit(errno)

      In this example, Python runs the program except if the criteria is not met, in which case Python calls the "sys.exit" function and returns the "errno" code. Use the "errno" code to debug your program.

Related Searches:

References

Comments

Related Ads

Featured