In this tutorial, we will guide you on how to exit a Python script running in the terminal. Exiting a Python script can be helpful if you need to halt the execution of a script or gracefully close down a script’s functions. We’ll go through several methods of exiting a Python script that suits various scenarios.
Method 1: KeyboardInterrupt – Using Ctrl+C
The most common method to exit a Python script running in a terminal is to issue a KeyboardInterrupt by pressing the CTRL+C keys simultaneously. When the script is running, press CTRL+C, and it will raise a KeyboardInterrupt exception, causing the script to terminate.
1 2 3 4 5 6 7 8 9 10 |
import time print("Python script started...") try: while True: print("Press CTRL+C to exit.") time.sleep(1) except KeyboardInterrupt: print("\nPython script exited.") |
Output:
1 2 3 4 5 6 |
Python script started... Press CTRL+C to exit. Press CTRL+C to exit. Press CTRL+C to exit. ^C Python script exited. |
Method 2: Using sys.exit()
Another method to exit a Python script is to use the sys.exit() function provided by the sys module. This function raises a SystemExit exception, and the script will terminate cleanly.
- First, import the sys module by adding the following line to the top of your script:
1 |
import sys |
- Then, add the following line where you want the script to exit:
1 |
sys.exit() |
1 2 3 4 5 6 7 8 9 10 |
import sys print("Python script started.") user_input = input("Type 'exit' to terminate: ") if user_input.lower() == 'exit': print("Python script exited.") sys.exit() else: print("You did not type 'exit'...moving forward in the script.") |
Output:
1 2 3 |
Python script started. Type 'exit' to terminate: exit Python script exited. |
Method 3: Using os._exit()
In situations where you need to force an immediate exit, you can use the os._exit() function provided by the os module.
This function does not raise any exceptions and terminates the script immediately, which can be useful in situations where the script might be stuck in an infinite loop.
- First, import the os module by adding the following line to the top of your script:
1 |
import os |
- Then, add the following line where you want the script to exit:
1 |
os._exit(0) |
The _exit() function requires an exit_code as an argument. The exit_code is an integer value that indicates the reason for exiting the script. A value of 0 indicates a successful exit, while any other value indicates an error.
1 2 3 4 5 6 7 8 9 10 |
import os print("Python script started.") user_input = input("Type 'exit' to terminate: ") if user_input.lower() == 'exit': print("Python script exited.") os._exit(0) else: print("You did not type 'exit'...moving forward in the script.") |
Output:
1 2 3 |
Python script started. Type 'exit' to terminate: exit Python script exited. |
Conclusion
In this tutorial, we covered three different methods to exit a Python script running in the terminal:
- KeyboardInterrupt – Using Ctrl+C
- Using sys.exit()
- Using os._exit()
Each of these methods serves its purpose depending on the specific scenario or outcome you need. Choose the suitable method for your script according to your requirements and use it to gracefully exit your Python scripts.