Difference Between Exit(0) and Exit(1) in Python

Difference between exit(0) and exit(1) in Python

0 and 1 are the exit codes.

exit(0) means a clean exit without any errors / problems

exit(1) means there was some issue / error / problem and that is why the program is exiting.

This is not Python specific and is pretty common. A non-zero exit code is treated as an abnormal exit, and at times, the error code indicates what the problem was. A zero error code means a successful exit.

This is useful for other programs, shell, caller etc. to know what happened with your program and proceed accordingly.

I want to know what exactly sys.exit(-1) returns in python?

sys.exit(-1) tells the program to quit. It basically just stops the python code from continuing execution. -1 is just the status code that is passed in. Generally 0 denotes successful execution, any other number (usually 1) means something broke.

Is there a difference between exit(0) and exit(True) in Python?

Using sys.exit(True) suggests to the user that the script exited with an error because it's non-zero:

Ξ /tmp → cat true_exit.py
import sys
sys.exit(True)
Ξ /tmp → python true_exit.py
↑1 /tmp → echo $?
1

If you want to use True/False instead of exit numbers, you can either define your own constants or use True/False:

Ξ /tmp → cat false_exit.py
import sys
sys.exit(False)
Ξ /tmp → python false_exit.py
Ξ /tmp → echo $?
0

Note that it may be helpful for other maintainers of your code if you use the same constants as defined in sysexits.h (probably available to you on *nix and OS X in /usr/include/sysexits.h).

Exit codes in Python

You're looking for calls to sys.exit() in the script. The argument to that method is returned to the environment as the exit code.

It's fairly likely that the script is never calling the exit method, and that 0 is the default exit code.

Python exit commands - why so many and when should each be used?

The functions* quit(), exit(), and sys.exit() function in the same way: they raise the SystemExit exception. So there is no real difference, except that sys.exit() is always available but exit() and quit() are only available if the site module is imported (docs).

The os._exit() function is special, it exits immediately without calling any cleanup functions (it doesn't flush buffers, for example). This is designed for highly specialized use cases... basically, only in the child after an os.fork() call.

Conclusion

  • Use exit() or quit() in the REPL.

  • Use sys.exit() in scripts, or raise SystemExit() if you prefer.

  • Use os._exit() for child processes to exit after a call to os.fork().

All of these can be called without arguments, or you can specify the exit status, e.g., exit(1) or raise SystemExit(1) to exit with status 1. Note that portable programs are limited to exit status codes in the range 0-255, if you raise SystemExit(256) on many systems this will get truncated and your process will actually exit with status 0.

Footnotes

* Actually, quit() and exit() are callable instance objects, but I think it's okay to call them functions.

What is difference between sys.exit(0) and os._exit(0)

According to the documentation:

os._exit():

Exit the process with status n, without calling cleanup handlers, flushing stdio buffers, etc.

Note The standard way to exit is sys.exit(n). _exit() should normally only be used in the child process after a fork().



Related Topics



Leave a reply



Submit