Home » How to Exit a Program in Python

How to Exit a Program in Python

by Online Tutorials Library

How to Exit a Program in Python

One of the most dynamic and adaptable programming languages available at present is no doubt Python. It is, without a doubt, the most widely used programming language today. Python provides the option and permission for a programmer to terminate a Python program at any time.

Method 1: Using quit() Function

To exit a Python application, utilize the built-in quit() function provided by the Python functions.

When the system meets the quit() function, then it entirely terminates the program’s execution.

Input:

Output:

23  

The interpreter meets the quit() function after the very first iteration, as shown above, of the for loop, the program is terminated.

Method 2: Python sys.exit() Function

The sys.exit() function in the Python sys module can be used to terminate a program and exit the execution process. The sys.exit() function could be invoked at any moment without worrying about the code becoming corrupted.

Syntax of this function is:

To further comprehend the sys.exit() function, consider the following example.

Input

Output:

An exception has occurred, use %tb to see the full traceback.    SystemExit: Values are not matching unfortunately  

Method 3: Using exit() Function

Apart from the strategies stated above, we can utilize Python’s built-in exit() function to exit and exit the program’s execution loop.

Syntax of the function is:

Input:

Output:

23  

Apart from the strategies stated above, we can utilize Python’s built-in exit() function to exit and exit the program’s execution loop.

Method – 4 Using the command- KeyboardInterrupt

If we execute a Python program on the console, hitting CTRL + C on Windows or CTRL + Z on Linux will cause the main thread to throw a KeyboardInterrupt exception.

If the Python program somehow doesn’t catch the exception, then the program will terminate. If we use except to catch this exception, our Python application might not quit.

If KeyboardInterrupt somehow doesn’t work, we can utilize the SIGBREAK signal on Windows by typing CTRL + PAUSE/BREAK.

To terminate a Windows process, use the taskkill command in Windows. We may also use task manager to locate python.exe and terminate it. The Python application will be terminated instantly.

Type the command below to kill a function by its PID::

Method – 5 Using the command – raise SystemExit

Simply put, the main purpose of the raise keyword is really to raise the exception. We can specify the type of mistake we want to raise. The SystemExit function’s base class is the BaseException class. Because the SystemExit method is derived from the BaseException, it can avoid being captured by the code that handles all exceptions. In Python code, the SystemExit function is called as follows:

Input

Output:

10  11  12  13  14  15  The program is terminated as BaseException is detected  

SystemExit can be thought of as an exception which is thrown by some of the Python exit routines discussed above. Furthermore, the quit() as well as sys.exit() exit routines throw a SystemExit exception when the program is terminated.

Method – 6 Using the os._exit(0) function

In Python, the os module provide functions for communicating with the operating system fluently. One of Python’s standard utility modules is the os module. This module gives us a quick and easy way to access operating system-specific features. Once we import the os module into Python code, the os. exit() method can be used. The os.exit() method can be used to exit a process with a specific status without needing to use cleanup handlers, flush stdio buffers, or other methods. And after os.fork() system function, it’s usually used during child process.

Input

Conclusion

In operational and production code, the exit() as well as quit() functions are not allowed. Because only the site module can be used to implement these two functions.


You may also like