236

What's the difference between exit(0) and exit(1) in Python?

I tried looking around but didn't find a specific question on these lines. If it's already been answered, a link would be sufficient.

the
  • 21,007
  • 11
  • 68
  • 101
seeker
  • 6,841
  • 24
  • 64
  • 100
  • 18
    Here's a link to [`exit()` in the docs](https://docs.python.org/3/library/constants.html#constants-added-by-the-site-module), which took me forever to find. – Ryne Everett Apr 04 '14 at 02:08
  • 5
    keep in mind: [`The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace. They are useful for the interactive interpreter shell and should not be used in programs`](https://docs.python.org/3/library/constants.html#constants-added-by-the-site-module), instead, use [`sys.exit()`](https://docs.python.org/2/library/sys.html#sys.exit) – Udi May 07 '14 at 18:12
  • 3
    Just realised there is `exit(0)` which is discussed in this question and also one with an underscore `_exit(0)`, the difference is explained [here](https://stackoverflow.com/questions/9591350/what-is-difference-between-sys-exit0-and-os-exit0) – cardamom Feb 28 '18 at 15:17
  • 1
    The nice thing about these codes is that they can be used directly in an `if` statement in a `bash` wrapper, so further action can be taken whether or not the Python program succeeds or whether it raised a non-zero status. – SDsolar May 05 '18 at 17:15
  • weird, if 0 is False and 1 is True in Python, you'd think the numbers would mean the opposite – Monica Heddneck Oct 05 '18 at 02:37
  • @MonicaHeddneck It's for historical reasons, and because there's only really one way to say "Everything went fine!", while there might be 255 or more ways to say *how* things went wrong. Thus "0" is the universal "Success" code, and everything else is "Here's a short code you can look up in the manual for this program to learn *what* went wrong". – coredumperror Oct 29 '21 at 22:38

5 Answers5

349

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.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 12
    And what about exit(-1) ? – Faizan Apr 01 '14 at 10:46
  • 19
    @Faizan: The exit code is an 8-bit value on Unix. If you invoke `exit(-1)`, the value is equivalent to `exit(255)` - the least significant 8 bits are relayed to the calling program (shell or whatever). – Jonathan Leffler Feb 06 '15 at 15:46
  • 7
    Note that according to [the docs](https://docs.python.org/3/library/constants.html#constants-added-by-the-site-module), `exit()` is added by the `site` module, and should not be used by programs. Instead, use `sys.exit()`, or even `raise SystemExit`, if you don't want to import another module. – daviewales Aug 22 '19 at 05:00
  • 2
    Because of exit codes from Unix/Linux, I was thinking 0 is True and 1 is False value in the Python conditions. – vlyalcin Apr 08 '20 at 15:36
  • any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0–127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs ... use 2 for command line syntax errors and 1 for all other errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. [sys module docs](https://docs.python.org/3/library/sys.html#sys.exit) – user7568519 Dec 07 '20 at 12:15
  • 1
    i like to think of it as `0` for 0 errors during execution – cryanbhu Jan 20 '21 at 10:28
  • The one thing I couldn't find out, that if there is a problem with the python code (syntax, unhandled exception), the interpreter always returns with 1 or there ore some other error statuses? I know I can use different numbers for different reasons e.g. sys.exit(42) for question.txt not found, sys.exit(3) for Earth (and so the Internet connection) was destroyed by the Vogons... – Arpad Horvath -- Слава Україні Feb 03 '22 at 09:21
22

This determines the exit status of the program when it finishes running (generally, 0 for success and 1 for error).

It is not unique to Python, and the exact effect depends on your operating system and how the program is called (though 99% of the time, if you're just running Python scripts, it doesn't matter).

David Robinson
  • 77,383
  • 16
  • 167
  • 187
17

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). Sometimes, that will be treated as signed (so you might see -128, -127, etc) but more usually it is treated as unsigned.

This status is available to the code that invoked Python. This convention applies across platforms, though the meaning of non-zero exit status can vary on different platforms.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
11

exit(0): This causes the program to exit with a successful termination.

exit(1): This causes the program to exit with a system-specific meaning.

On many systems, exit(1) signals some sort of failure, however there is no guarantee.

As I recall, the C standard only recognizes three standard exit values:

  • EXIT_SUCCESS -- successful termination
  • EXIT_FAILURE -- unsuccessful termination
  • 0 -- same as EXIT_SUCCESS
Community
  • 1
  • 1
8

The number you pass to the exit() function is simply your program's return code, which is given to the operating system. From your program's point of view, there is no difference: execution will end in both cases, and the value supplied to the function will be given to the OS. But some tools and scripts take into account the program's exit code. Most tools return 0 when they succeed and nonzero to indicate an error.

So, if your program will be run from a script, an automated tool or from some other software that takes into account the return code (such as an IDE), you must be careful on what you return.

When in doubt, just return 0 to indicate everything is OK.

the
  • 21,007
  • 11
  • 68
  • 101