I think you can use
sys.exit(0)
You may check it here in the python 2.7 doc:
Answer from godidier on Stack OverflowThe optional argument arg 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” by shells and the like.
I think you can use
sys.exit(0)
You may check it here in the python 2.7 doc:
The optional argument arg 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” by shells and the like.
you didn't import sys in your code, nor did you close the () when calling the function... try:
import sys
sys.exit()
Return vs sys.exit()
Python won't spit out sys.exit message in try function
Is it better to quit a script due to a user input error using sys.exit(1) or raise Error()?
Qwen3.5-397B-A17B run in dual spark! but I have a concern
Videos
In most questions asking how to stop code the recommended answer is sys.exit() or raising an exception. Why is exit() not suggested given it is simpler, not requiring import sys, and it does the same thing underneath?
e.g. https://www.reddit.com/r/learnpython/comments/hv7phs/how_do_i_stop_a_code/?utm_medium=android_app&utm_source=share)
Let's say you have a script that is a simple csv parser but the user enters an invalid filepath for the csv.
Is it better to exit the script with a print("wrong filepath") and then sys.exit(1) or by using raise SomeError("wrong filepath")?
From what I read sys.exit(1) raises an exception also, but it doesn't seem to print a traceback like using raise SomeError("wrong filepath") does. So you can just print the message you want the user to see and exit quietly.
If I were to distribute a script like this to public users. What's the better practice?