Python Exit Program: How to Stop Code Execution
There are two ways to stop running code in Python:
- Exit the system.
- Terminate the program.
- terminate the program
- System
- system
import sys
# 停止运行代码
sys.exit()
- exit the system
- terminate the system
- Exit the system
# 停止运行代码
raise SystemExit
Both of these methods can immediately stop the program from running, but it is important to note that they will directly terminate the program without performing any cleanup or closing operations, so caution is advised when using these methods.