Is it possible to break the execution of a Python script called with the execfile function without using an if/else statement? I\'ve tried exit()
, but it doesn\'t a
What's wrong with plain old exception handling?
scriptexit.py
class ScriptExit( Exception ): pass
main.py
from scriptexit import ScriptExit
print "Main Starting"
try:
execfile( "script.py" )
except ScriptExit:
pass
print "This should print"
script.py
from scriptexit import ScriptExit
print "Script starting"
a = False
if a == False:
# Sanity checks. Script should break here
raise ScriptExit( "A Good Reason" )
# I'd prefer not to put an "else" here and have to indent the rest of the code
print "this should not print"
# lots of lines below