Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does python have an EXIT_SUCCESS constant?

Tags:

python

c

I'm returning 0 all over the place in a python script but would prefer something more semantic, something more readable. I don't like that magic number. Is there an idea in python similar to how in C you can return EXIT_SUCCESS instead of just 0?

I was unable to find it here: https://docs.python.org/3.5/library/errno.html

like image 543
tarabyte Avatar asked Mar 05 '16 19:03

tarabyte


People also ask

How do you write exit 0 in Python?

The standard convention for all C programs, including Python, is for exit(0) to indicate success, and exit(1) or any other non-zero value (in the range 1.. 255) to indicate failure. Any value outside the range 0.. 255 is treated modulo 256 (the exit status is stored in an 8-bit value).

What is exit code 2 in Python?

An error code of 2 is usually (not always) file not found. This to me would suggest that your python script isn't correctly picking up the file. Are you using a relative path in your script? As it may be that when you run it from the cmd line you are in a different location and that is why it works.

What is quit () in Python?

The quit() Function Python's in-built quit() function exits a Python program by closing the Python file. Since the quit() function requires us to load the site module, it is generally not used in production code.

What is EXIT_SUCCESS?

Explanation. EXIT_SUCCESS. successful execution of a program. EXIT_FAILURE. unsuccessful execution of a program.


2 Answers

I'm returning 0

return is not how you set your script's exit code in Python. If you want to exit with an exit code of 0, just let your script complete normally. The exit code will automatically be set to 0. If you want to exit with a different exit code, sys.exit is the tool to use.

If you're using return values of 0 or 1 within your code to indicate whether functions succeeded or failed, this is a bad idea. You should raise an appropriate exception if something goes wrong.

like image 162
user2357112 supports Monica Avatar answered Sep 28 '22 01:09

user2357112 supports Monica


Since you discuss return here, it seems like you may be programming Python like C. Most Python functions should ideally be written to raise an exception if they fail, and the calling code can determine how to handle the exceptional conditions. For validation functions it's probably best to return True or False - not as literals, usually, but as the result of some expression like s.isdigit().

When talking about the return value of a process into its environment you caonnt use return because a module isn't a function, so a return statement at top level would be flagged as a syntax error. Instead you should use sys.exit.

Python might seem a little minimalist in this respect, but a call to sys.exit with no arguments defaults to success (i.e. return code zero). So the easiest way to simplify your program might be to stop coding it with an argument where you don't want to indicate failure!

As the documentation reminds us, integer arguments are passed back, and string arguments result in a return code of 1 and the string is printed to stderr.

The language doesn't, as far as I am aware, contain any constants (while it does have features specific to some environments, if it provided exit codes their values might need to be implementation- or platform-specific and the language developers prefer to avoid this where possible.

like image 20
holdenweb Avatar answered Sep 28 '22 00:09

holdenweb