Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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)

Solution 2:

you are using the sys module, but you haven't imported it.

add this before the sys.exit() line :

import sys

Post a Comment for "How Can I Have A Python Script Safely Exit Itself?"