Introduction to Python exit script methods
Python scripts or programs run forever unless the execution reaches the end of the program or is exited explicitly. Python provides different ways and methods to quit from script or program. In this tutorial, we will learn how we can explicitly exit the python program by using different methods.
We will learn about exit()
, sys.exit()
, and quit()
methods and will discuss how they exit the python program by taking various examples. At the same time, we will also cover how we can exit the program in the middle of a function and when a condition is satisfied. In a nutshell, this tutorial will contain all the necessary methods that you need to know in order to exit the python program.
Getting started with Python exit script
Python exit script refers to the process of termination of an active python process. It is simply a call to a function or destructor to exit the routines of the program. This process is done implicitly every time a python script completes execution, but could also be invoked by using certain functions. In the following sections, we will discuss some of these functions by taking various examples.
Python exit script using exit() method
The Pythonexit()
method is also included inside the site module of Python. The site module is the target directory of manually built Python packages. When we build and install Python packages from the source, we will find the installed modules in the site module/packages by default. The following is the simple syntax of the Python
exit()
method.
exit()
The exit method does not take any mandatory argument. All the Python statements after the exit()
method will not be executed.
Examples of Python exit script using exit() method
Now let us take an example and see how the python exit() method works. See the python program below:
# printing
print("Welcome to www.golinuxcloud.com")
# Python exit script using exit() method
exit()
# printing
print("Welcome again!!!")
Output:
Welcome to www.golinuxcloud.com
Notice that the Python statement after the exit()
method was not executed. If we will use the exit()
method inside a loop, then the execution of the loop will stop after executing the exit()
method. See the example below:
# Python while loop
while True:
# printing
print("Welcome to golinuxcloud")
# Python exit script using exit method
exit()
Output:
Welcome to golinuxcloud
Although the loop was an infinite loop, it stopped executing once it reached to the exit()
method.
Python exit script using sys.exit( ) method
The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment. It allows operating on the interpreter as it provides access to the variables and functions that interact strongly with the interpreter. One of the methods available in this module is the exit method that ends the execution of the Python script. The following is the simple syntax of this method.
sys.exit()
This method also does not require any argument.
Examples of Python exit script using sys.exit() method
Now let us take an example and see how the sys.exit()
method works. See the Python program below:
# importing sys module
import sys
# printing
print("Welcome to golinuxcloud")
# Python exit script using exit method
sys.exit()
# printing
print("Welcome to golinuxcloud")
Output:
Welcome to golinuxcloud
Notice that the python script stopped executing after the sys.exit()
method has been executed. If we will use the same method inside any python loop, then it will stop too, after executing the sys.exit()
method. See the example below:
# importing sys module
import sys
# Python for loop
for i in range(100):
# printing
print("Welcome to golinuxcloud")
# Python exit script using exit method
sys.exit()
Output:
Welcome to golinuxcloud
Notice that the loop was supposed to execute 100 times but because of the sys.exit()
method, it stopped executing.
Python exit script using quit() method
Another built-in method to exit a python script is quit()
method. When the program encounters the Python quit()
function in the system, it terminates the execution of the program completely. The following is the simple syntax of the quit()
method.
quit()
This method also does not require any argument as it is used to terminate the python script completely.
Examples of Python exit script using quit() method
Now let us take an example and see how the Python quit()
method works. See the python program below:
# printing
print("Welcome to golinuxcloud")
# Python exit script using quit method
quit()
# printing
print("Welcome to golinuxcloud")
Output:
Welcome to golinuxcloud
Notice that the python statement after the quit()
method was not executed because the quit()
method has terminated the program. If we will use this method inside any of the python loops, then the program will terminate once this method is executed. See the following example.
# python for loop
for i in range(100):
# printing
print("Welcome to golinuxcloud")
# Python exit script using quit method
quit()
Output:
Welcome to golinuxcloud
Notice that even the loop was supposed to be executed 100 times but because of the quit()
method, it terminates.
Python exit script in a function
A Python function is a group of code. To run the code in a function, we must call the function. A function can be called from anywhere after the function is defined and it can return a value using a return statement. In this section, we will see how we can use the python exit script inside the Python function.
Example of python exit script in the return statement
Now let us see how we can use the Python exit script as a return statement in our function. See the example below:
# Python function
def main(bol):
# for loop
for i in range(10):
# break the range if the condition is true
if bol is True:
# Python break
break
# else
else:
# printing
print("System exit:")
# return exit
return exit()
# calling the function
main(False)
Output:
System exit:
Notice that the condition was false so the else part of the function was executed which returns exit and terminates the program.
Example of Python exit script in the middle of a function
If we want to terminate the function in middle without using a return statement, we can do it. See the python program below which terminates the function in the middle without using any return statement.
# Python function
def main(bol):
# for loop
for i in range(10):
# break the range if the condition is true
if bol is True:
# Python break
break
# else
else:
# printing
print("System exit:")
# exit
exit()
# calling the function
main(False)
Output:
System exit:
Notice that the function did not have any return statement but still uses the exit method and terminates the program.
Python exit script when a condition is satisfied
If we have a loop in our Python code and we want to make sure the code can exit if it encounters a problem, we can use a flag/condition that it can check to terminate the program. See the python program below:
# Python function
def main(bol):
condition = False
# for loop
while True:
# increment the number
bol+=1
# print
print(bol)
# if statement to check the condition
if bol%5==0:
# condition become True
condition = True
# check if condition is true
if condition:
# exit
exit()
# calling the function
main(1)
Output:
2
3
4
5
Notice that the loop was terminated when the condition becomes true and the exit method was executed.
Python exit script with a message
As we already discussed in the above sections that the exit method does not take any mandatory arguments, however, the exit()
method can take an optional argument. It takes a string as an optional argument and then prints it out. See the example below:
# Python function
def main(bol):
condition = False
# for loop
while True:
# increment the number
bol+=1
# print
print(bol)
# if statement to check the condition
if bol%5==0:
# condition become True
condition = True
# check if condition is true
if condition:
# exit with message
exit("The condition was satisfied and exited")
# calling the function
main(1)
Output:
2
3
4
5
The condition was satisfied and exited
Notice that we have passed a string as an argument to the exit method and it prints it out after executing. The same is with quit()
and sys.exit()
methods. They take a string as an optional argument and print it out. See the Python program below using each of them.
# printing
print("program stated!!")
# exit using quit() method
quit("Program was terminated")
# printing
print("program ended!!")
Output:
program stated!!
Program was terminated
Now let us take another example using sys.exit()
method along with a message. See the program below:
# importing module
import sys
# printing
print("program stated!!")
# exit using sys.exit() method
sys.exit("Program was terminated")
# printing
print("program ended!!")
Output:
program stated!!
Program was terminated
Notice that the message which was provided as an argument to the methods is printed.
Summary
Exiting a Python script refers to the process of termination of an active python process. In this tutorial, we learned about three different methods that are used to terminate the python script including exit()
, quit()
and sys.exit()
method by taking various examples. At the same time, we also discussed how we can exit a function in pythons. Moreover, we also covered how we can exit a python script along with a message. All in all, we covered all the necessary methods and examples that are important to exit the python script.
Further Reading
Python sys module
Python program control
Python sys.exit() method