Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have a python script safely exit itself?

Heres the scenario I have a password that must be entered if entered wrong the script will not proceed and just exit itself? But how can I tell the script to safely exit itself?

I tried sys.exit() but that gives a traceback error and doesn't seem like a very clean exit method.

like image 268
harry Avatar asked Oct 30 '25 03:10

harry


1 Answers

In fact, sys.exit() will only throw a SystemExit exception, which would make the program exit if this exception wasn't catched, but I don't think it's poor coding.

Anyway, another possibility is to use os._exit(0) if you need to exit immediately at any cost. (cf. http://docs.python.org/2/library/exceptions.html#exceptions.SystemExit)

like image 103
rafa Avatar answered Oct 31 '25 17:10

rafa