By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

Python command to exit script

P: 2
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.
Feb 17 '16 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 621
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
Expand|Select|Wrap|Line Numbers
  1. def test_func(exit_now):
  2.     print "this is always executed"
  3.     if exit_now:
  4.         return
  5.     print "this only prints if exit_now is False"
  6.  
  7. if __name__ == "__main__":
  8.     test_func(True)
  9.     print "-"*50
  10.     test_func(False) 
Feb 17 '16 #2

P: 2
What if the commands aren't in a function?
Feb 17 '16 #3

Expert 100+
P: 621
Then put them in one. Don't beat your head against the wall for no reason.
Feb 18 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.