Why does Python automatically exit a script when it’s done?
Last Updated :
26 Nov, 2020
Python is a scripting language. This means that a Python code is executed line by line with the help of a Python interpreter. When a python interpreter encounters an end-of-file character, it is unable to retrieve any data from the script. This EOF(end-of-file) character is the same as the EOF that informs the end of the file while reading data from a file in Python.
There is an EOF character present at the end of each python script that instructs the interpreter to stop the execution of code. While working on a standard input connected to a tty device, we can produce a similar result with CTRL+D on UNIX and CTRL+Z, ENTER on Windows.
Consider this simple code that takes a number as user input and returns twice of that number.
Python3
a = int(input("Enter number: "))
# waiting for user input...
print("Twice of the number: ", 2 * a)
Try pressing the EOF character(depending on your os) while the program is waiting for user input. The program will terminate as soon as it encounters the EOF character like this:
Exiting Python Script
Notice that the program throws an EOFError Exception when it encounters EOF before the program completion.
How to Detect Script Exit?
We can use the atexit module to detect the script exit. The following code detects script exit using register() function of the atexit module.
Syntax: atexit.register(fun, *args, **kwargs)
Parameters: First the function name is mentioned and then any arguments for that function is passed. The parameters are separated using ‘, ‘.
Return: This function returns the called fun and hence the calling can be traced.
The atexit.register() method takes a function as an argument that is to be executed at script exit.
Python3
import atexit
a = 5
print("Twice of a:", a*2)
atexit.register(print, "Exiting Python Script")
Output:
Twice of a: 10
Exiting Python Script
Similar Reads
Detect script exit in Python Python is a scripting language. This means that a Python code is executed line by line with the help of a Python interpreter. When a python interpreter encounters an end-of-file character, it is unable to retrieve any data from the script. This EOF(end-of-file) character is the same as the EOF that
2 min read
Python exit commands: quit(), exit(), sys.exit() and os._exit() The functions quit(), exit(), sys.exit(), and os._exit() have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exits and no stack traceback is printed. We can catch the exception to intercept early exits and perform cleanup activities; if uncaught,
6 min read
How to Exit a Python script? In this article, we are going to see How to Exit Python Script. Exiting a Python script refers to the termination of an active Python process. In this article, we will take a look at exiting a Python program, performing a task before exiting the program, and exiting the program while displaying a cu
4 min read
How does the Python Interpreter check thread duration? In Python, threads are a means of achieving concurrent execution. The Python interpreter employs a mechanism to manage and monitor the duration and execution of threads. Understanding how the Python interpreter handles thread duration is essential for developing efficient and responsive multithreade
3 min read
How to check any script is running in linux using Python? Python is a strong and exponentially growing programming language in the present day. There is a multiple-way to check which script is running in the background of a Linux environment. One of them is using the subprocess module in python. Subprocess is used to run new programs through Python code by
2 min read