Python is the most popular programming language used nowadays for building any type of website, application, or software. To build the python website or application you have to write the python script, and you may also have a situation where you want to stop the program or script.
So in this tutorial, we will learn how to stop a python program and exit from the script. We will use 4 python commands to exit the program.
All commands have almost the same functionality behavior to raise the SystemExit exception. Normally when the python program is run then it executes from the top and exits at end of the script.
But python also provides us to use the built-in functions to use for stop the program and exit from the script. You just have to use those functions in your code where you want to stop the execution of the script.
So let’s get started learning about python exit commands.
4 Python Commands to Exit Program
Those 4 commands are quit(), exit(), sys.exit() and os._exit(). We will go through one-by-one commands and see how to use them.
Using quit() function
The quit()
function is a built-in function provided by python and use can use it to exit the python program. It will stop the execution of the script where you call this function.
Syntax:
Example
When you run the above program then it will print the output once that is x
multiplied by 2
and then it will return the quit message. You can see the following output of the code.
Output
10 Traceback (most recent call last): File "", line 3, in File "/usr/lib/python3.8/_sitebuiltins.py", line 26, in __call__ raise SystemExit(code) SystemExit: None
Using exit() function
There is another function to exit the python program that is the exit()
function. This is an alternate of quit() function and uses it to make python code more friendly.
Syntax:
Example
It will also print the exit message when you run the above program. And return the same result as the above example output.
Output
10 Traceback (most recent call last): File "", line 3, in File "/usr/lib/python3.8/_sitebuiltins.py", line 26, in __call__ raise SystemExit(code) SystemExit: None
Using sys.exit() function
The sys.exit()
function is a built-in function of the sys
module and also used to stop the execution of python programming script.
To use the sys.exit()
function, you have to import the sys
module in your program file, and then you can use the sys
module’s function.
Syntax:
Example
It will return the following output while executing.
Output
Value of x do not match
Using os._exit() function
Python has an os
module one of the standard utility modules. You can use this os
module to utilize the operating system functionality.
So, the os._exit()
method can be utilized to exit the process with specified status without having the need to call cleanup handlers, flushing stdio buffers, etc.
You have to import the os
module in your python file in order to use the os._exit() function and stop the python program and exit from the script.
Syntax:
Example
It will go through the script code and print the message according to the process check condition.
Output
In the child process Process ID: 186 Bye! exiting... Parent process Child's exit code: 0
So that’s it!!!
Conclusion
So you learned in this article, how you can stop the execution of the python program and exit from the script using the 4 python commands functions.
So the point is that you cannot use the quit() and exit() function in the production codes because these functions can only be implemented when the site module is imported.
Therefore, from the above four functions, the most preferred function is sys.exit() function. So if you have questions comment below, I’ll help you with that.
See Also: Python rang() Function – Reverse, Increment, Decrement