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()
Difference between exit() and sys.exit() in Python - Stack Overflow
Why is sys.exit() recommended over exit() or quit()
argparse and exit codes - bleep.py
argparse doesn't give you any control over the exit codes, but you could use something like this?
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument('foo')
try:
args = parser.parse_args()
except SystemExit:
sys.exit(1) More on reddit.com Videos
exit is a helper for the interactive shell - sys.exit is intended for use in programs.
The
sitemodule (which is imported automatically during startup, except if the-Scommand-line option is given) adds several constants to the built-in namespace (e.g.exit). They are useful for the interactive interpreter shell and should not be used in programs.
Technically, they do mostly the same: raising SystemExit. sys.exit does so in sysmodule.c:
static PyObject *
sys_exit(PyObject *self, PyObject *args)
{
PyObject *exit_code = 0;
if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
return NULL;
/* Raise SystemExit so callers may catch it or clean up. */
PyErr_SetObject(PyExc_SystemExit, exit_code);
return NULL;
}
While exit is defined in site.py and _sitebuiltins.py, respectively.
class Quitter(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return 'Use %s() or %s to exit' % (self.name, eof)
def __call__(self, code=None):
# Shells like IDLE catch the SystemExit, but listen when their
# stdin wrapper is closed.
try:
sys.stdin.close()
except:
pass
raise SystemExit(code)
__builtin__.quit = Quitter('quit')
__builtin__.exit = Quitter('exit')
Note that there is a third exit option, namely os._exit, which exits without calling cleanup handlers, flushing stdio buffers, etc. (and which should normally only be used in the child process after a fork()).
If I use exit() in a code and run it in the shell, it shows a message asking whether I want to kill the program or not. It's really disturbing.
See here
But sys.exit() is better in this case. It closes the program and doesn't create any dialogue box.
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)