In Python to terminate any script, you can use the sys module:-
import sys
sys.exit()
The above code can be used to terminate any script.
sys.exit([arg]):-
This function is used to exit from Python. This is implemented by raising the SystemExit exception.
Talking about the optional argument arg, this can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination”.
When an error occurs then sys.exit("some error message") is a quick way to exit a program.
The exit() function ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.
The main and very important reason why you should prefer sys.exit because it is more "friendly" to other code, all it actually does is raise an exception.
To know more about this you can have a look at the following video tutorial:-