You're looking for calls to sys.exit(...) (exit(...) calls 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.

Answer from Dave Costa on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-exit-commands-quit-exit-sys-exit-and-os-_exit
Python exit commands: quit(), exit(), sys.exit() and os._exit() - GeeksforGeeks
July 12, 2025 - If you print it, it will give a message and end a program in Python. Example: In the provided code, when i is equal to 5, it prints "quit" and attempts to exit the Python interpreter using the quit() function.
Discussions

How to throw error and exit with a custom message in python - Stack Overflow
I've seen people suggesting sys.exit() in Python. My question is that, is there any other way to exit the execution of current script, I mean termination, with an error. Something like this: sys.... More on stackoverflow.com
🌐 stackoverflow.com
How to get an exit code in Python? - Stack Overflow
I am studying about exceptions and exit codes for errors but my terminal does not give me an exit code in Python 3.8 on Windows 10. I would like to be able to se the message "Process finished with exit code 0" from a simple "hello world" program for example. More on stackoverflow.com
🌐 stackoverflow.com
Difference between exit(0) and exit(1) in Python - Stack Overflow
Just realised there is exit(0) ... an underscore _exit(0), the difference is explained here ... 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 ... More on stackoverflow.com
🌐 stackoverflow.com
Pause a running python script and resume when necessary.
On Linux or Mac you could press Ctrl-z which will suspend the process from running and then run the command "fg" to continue the process. The other option that I can see is to save the progress of the script on exit and be able to load it again next time it's run. It depends on if you are taking input from the user or if you'll have to catch Ctrl-C. More on reddit.com
🌐 r/Python
7
3
December 12, 2013
🌐
Codecademy
codecademy.com › article › python-exit-commands-quit-exit-sys-exit-os-exit-and-keyboard-shortcuts
Python Exit Commands: quit(), exit(), sys.exit(), os._exit() and Keyboard Shortcuts | Codecademy
Intended only for use in the Python interpreter or shell sessions. It is a helper function provided by the site module. If used in a standalone script, it can raise a NameError since the site module may not be loaded. ... This code prints even numbers starting from 2 and exits the program using quit() when it reaches the fifth even number, which is 10.
🌐
Linux Hint
linuxhint.com › python-exit-codes
Python Exit Codes – Linux Hint
In the example program above, we start by importing the exit function from the sys module. We then use the print statement to print some text on the screen. For the important part, we use the exit function and pass the exit code as 125. NOTE: As soon as Python encounters the exit() function, it will immediately terminate the program and return the exit code or message specified.
🌐
Delft Stack
delftstack.com › home › howto › python › exit codes in python
How to Exit Codes in Python | Delft Stack
March 11, 2025 - To utilize exit codes in Python, you typically use the sys.exit() function from the sys module. This function allows you to specify the exit code you want your script to return. Here’s a simple example to illustrate this concept:
🌐
Super Fast Python
superfastpython.com › home › tutorials › process exit codes in python
Process Exit Codes in Python - Super Fast Python
September 11, 2022 - You can set an exit code for a process via sys.exit() and retrieve the exit code via the exitcode attribute on the multiprocessing.Process class. In this tutorial you will discover how to get and set exit codes for processes in Python. Let’s get started. Need Process Exit Codes A process is a running instance of […]
🌐
Notes
henryleach.com › 2025 › 02 › controlling-python-exit-codes-and-shell-scripts
Controlling Python Exit Codes and Shell Scripts - Henry Leach
February 9, 2025 - $ python exit-examples.py; echo Returned $? Hello World Returned 1 · You could of course return any value you like between 0 and 255, but other than 0 being success, there's not much agreement what the various exit codes mean.
Find elsewhere
🌐
Python Morsels
pythonmorsels.com › exiting-a-python-program
Exiting a Python program - Python Morsels
February 21, 2022 - If you want to exit a Python program early, call the sys.exit function or raise a SystemExit exception. If you're trying to indicate an error code while exiting, pass a number from 0 to 127 to sys.exit, where 0 indicates success and anything else indicates an error.
🌐
Quora
quora.com › What-is-the-exit-status-code-of-a-Python-script
What is the exit status code of a Python script? - Quora
Answer (1 of 2): In Python, you can check the exit code of a script using the `sys.exit()` function provided by the `sys` module. By default, if your script runs without any issues, it will have an exit code of 0 (zero), which indicates success . The exit codes only have meaning as assigned by th...
🌐
Medium
medium.com › @anupkumarray › working-with-exit-codes-between-python-shell-scripts-177931204291
Working with exit codes between Python & Shell Scripts | by Anup Kumar Ray | Medium
October 17, 2021 - Since exception is properly handled in python code, it will exit with a success return code 0 when Exceptions.sh is executed.
🌐
freeCodeCamp
freecodecamp.org › news › python-exit-how-to-use-an-exit-function-in-python-to-stop-a-program
Python Exit – How to Use an Exit Function in Python to Stop a Program
June 5, 2023 - In this example, the program will print "Before exit", but when the exit() function is called with a status of 1, the program will terminate immediately without executing the remaining code. Therefore, the line "After exit" will not be printed. Let's now write a Python script and demonstrate ...
🌐
AskPython
askpython.com › home › exit a python program in 3 easy ways!
Exit a Python Program in 3 Easy Ways! - AskPython
April 7, 2023 - Another useful function to exit ... into Python programs. An additional feature of this function is that it can take a string as an argument and print it after it is executed. The sys.exit() function can be used at any point in time without worrying about corruption in the code. ... Let us have a look at the below example to understand ...
🌐
CodeRivers
coderivers.org › blog › python-exit-code
Python Exit Code: Understanding, Using, and Best Practices - CodeRivers
March 5, 2025 - In this example, if a ZeroDivisionError occurs, the program prints an error message and exits with an exit code of 1. If no errors occur, it reaches the end and exits with an exit code of 0. When running a Python script directly from the command line, the return value of the main() function ...
🌐
HashBangCode
hashbangcode.com › article › stopping-code-execution-python
Stopping Code Execution In Python | #! code
Here is another example that stops execution if the length of an array is less than 2. import sys listofitems = [] # Code here that does something with listofitems if len(listofitems) < 2: sys.exit('listofitems not long enough') As a side note, if you are looking to simply kill a python program that has taken over your terminal then press Ctrl+C to stop execution.
🌐
Adam Johnson
adamj.eu › tech › 2021 › 10 › 10 › the-many-ways-to-exit-in-python
The Many Ways to Exit in Python - Adam Johnson
October 10, 2021 - If you’re looking for a quick answer, you can stop reading here. Use raise SystemExit(<code>) as the obviousest way to exit from Python code and carry on with your life.
🌐
O'Reilly
oreilly.com › library › view › programming-python-second › 0596000855 › ch03s04.html
Program Exits - Programming Python, Second Edition [Book]
March 1, 2001 - In fact, explicitly raising the built-in SystemExit exception with a Python raise statement is equivalent to calling sys.exit. More realistically, a try block would catch the exit exception raised elsewhere in a program; the script in Example 3-11 exits from within a processing function.
Author   Mark Lutz
Published   2001
Pages   1296
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › python exit commands
Python Exit Commands | Interview Kickstart
November 7, 2024 - Python exit commands explained: sys.exit(), quit(), Ctrl+D, and best practices for graceful program termination in scripts and REPL.