Python command to exit script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnegracejr
    New Member
    • Feb 2016
    • 2

    Python command to exit script

    I would like a command to end a python script "early" without completing the reset of the script but leave the IDLE shell open. I've tried several of the more common sense commands (kill exit sys.exit etc...) but they all close the interactive shell too (after a dialog box confirmation which I also don't want.

    I want this command to skip the rest of the script and exit to the IDLE interactive shell as if the rest of the lines in the script had been commented out.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    This is a fairly common thing that one does. See the following code which does or does not exit based on the value of exit_now
    Code:
    def test_func(exit_now):
        print "this is always executed"
        if exit_now:
            return
        print "this only prints if exit_now is False"
    
    if __name__ == "__main__":
        test_func(True)
        print "-"*50
        test_func(False)

    Comment

    • johnegracejr
      New Member
      • Feb 2016
      • 2

      #3
      What if the commands aren't in a function?

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Then put them in one. Don't beat your head against the wall for no reason.

        Comment

        Working...