Detect script exit in Python Last Updated : 12 Nov, 2020 Comments Improve Suggest changes Like Article Like Report 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. To detect a script exit, we can use the built-in the atexit library of Python. The atexit module is used to register or unregister functions that handle clean-up. The functions that are registered by atexit are automatically called at interpreter termination. 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 following example demonstrates how atexit can be used to detect script exit: Python3 import atexit n = 2 print("Value of n:",n) atexit.register(print,"Exiting Python Script!") Output: Value of n: 2 Exiting Python Script! In this simple program, we passed a print function and a string as arguments to atexit.register function. This registered the print statement as a function that will be invoked on script termination. We can also use register() method as a decorator. Python3 import atexit n = 2 print("Value of n:",n) # Using register() as a decorator @atexit.register def goodbye(): print("Exiting Python Script!") Output: Value of n: 2 Exiting Python Script! Python provides various functions that can be used to exit a python script, you can check them here. Comment More infoAdvertise with us Next Article Detect script exit in Python sareendivyansh Follow Improve Article Tags : Python python-utility python-basics Practice Tags : python Similar Reads 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 __exit__ in Python Context manager is used for managing resources used by the program. After completion of usage, we have to release memory and terminate connections between files. If they are not released then it will lead to resource leakage and may cause the system to either slow down or crash. Even if we do not re 3 min read User-defined Exceptions in Python with Examples In Python, exceptions are used to handle errors that occur during the execution of a program. While Python provides many built-in exceptions, sometimes we may need to create our own exceptions to handle specific situations that are unique to application. These are called user-defined exceptions.To m 4 min read Decrement in While Loop in Python A loop is an iterative control structure capable of directing the flow of the program based on the authenticity of a condition. Such structures are required for the automation of tasks. There are 2 types of loops presenting the Python programming language, which are: for loopwhile loop This article 3 min read wxPython | Exit() function in wxPython In this article we are going to learn about wx.Exit() which is a inbuilt parent function present in wxPython.Exit() function exits application after calling wx.App.OnExit . Should only be used in an emergency: normally the top-level frame should be deleted (after deleting all other frames) to termin 1 min read Python Try Except In Python, errors and exceptions can interrupt the execution of program. Python provides try and except blocks to handle situations like this. In case an error occurs in try-block, Python stops executing try block and jumps to exception block. These blocks let you handle the errors without crashing 6 min read Try, Except, else and Finally in Python An Exception is an Unexpected Event, which occurs during the execution of the program. It is also known as a run time error. When that error occurs, Python generates an exception during the execution and that can be handled, which prevents your program from interrupting.Example: In this code, The sy 6 min read How to capture SIGINT in Python? The signal module performs a specific action on receiving signals. Even it has the ability to capture the interruption performed by the user through the keyboard by use of SIGINT. This article will discuss SIGINT only, how to capture it, and what to do after it has been captured. Modules Required: S 3 min read How to check the execution time of Python script ? In this article, we will discuss how to check the execution time of a Python script. There are many Python modules like time, timeit and datetime module in Python which can store the time at which a particular section of the program is being executed. By manipulating or getting the difference betwee 6 min read Python - turtle.exitonclick() The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.exitonclick() : This function is used to Go into mainloop until the mouse is 2 min read Like