Is it possible to stop execution of a python script at any line with a command?
Like
some code quit() # quit at this point some more code (that's not executed)
To stop code execution in Python you first need to import the sys object. After this you can then call the exit() method to stop the program from running. It is the most reliable, cross-platform way of stopping code execution.
terminate() function will terminate foo function. p. join() is used to continue execution of main thread. If you run the above script, it will run for 10 seconds and terminate after that.
Graceful exit You can use the bash command echo $? to get the exit code of the Python interpreter.
The exit function is a useful function when we want to exit from our program without the interpreter reaching the end of the program. Some of the functions used are python exit function, quit(), sys. exit(), os. _exit().
sys.exit() will do exactly what you want.
import sys sys.exit("Error message")
You could raise SystemExit(0)
instead of going to all the trouble to import sys; sys.exit(0)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With