How do you exit python during execution of script?

Skip Montanaro skip at mojam.com
Mon Sep 13 18:48:22 EDT 1999


    >> import sys
    >> try:
    >>     print "normal operation"
    >>     sys.exit()        # I want to leave the script here
    >> except:
    >>     print "something went wrong"
    >>     sys.exit()        # instead, I dump out here...why?

Because that's how sys.exit() works, by raising SystemExit.  When it's not
caught, the normal behavior is for the system to exit quietly.  You might
try

    import sys
    try:
        print "normal operation"
        sys.exit()        # I want to leave the script here
    except:
        print "something went wrong"
        sys.exc_info()

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/~skip/
847-971-7098   | Python: Programming the way Guido indented...




More information about the Python-list mailing list