How to Exit in Python

(1 vote, average 5.00 out of 5)

Exiting in python is fairly straight-forward.  You need to import the "sys" library, and then call exit from it.

1
2
3
import sys
 
sys.exit()

Calling exit with no arguments is a clean exit -- no error code is returned.  If you'd like to exit with an error message, simply pass the message as a parameter and exit will automatically exit with an error and print your message.

1
2
3
import sys
 
sys.exit("This is an example of an error message")

 

Partner Links:
Last Updated on Tuesday, 15 June 2010 20:16  
Related Articles

» How to Change the Shutdown Button in the Windows 7 Start Menu

Changing the Shut Down button in the Windows 7 start menu is very easy!You can change this button to any of the following: Log offSleep Switch UserLockRestartShut downTo change the button follow these simple steps:Right click on the Windows 7 task bar. Select Properties.Select the Start Menu tab. You will see a window like the one below. Change the Power button action to the option you'd like.Hit the Apply button and close the window. Your power button on the start menu...

» Conditional (If-Else) Statements in Python

Below is a quick example of how to use an if statement in python: 12345if x==3: print "We are inside an if statement" x = x + 1print "we are no longer in an if statement" Note that unlike most languages, no parentheses are needed, although including parentheses does not violate python syntax.  Anything indented after the if statement is included in the execution if the if statement evaluates to true.  The scope is closed once indentation returns to the indent location of the...

» PHP Parse Error: Unexpected $

ErrorA PHP parse error returns, citing an "unexpected $".  Because this is a parse error, the PHP script does not run to any extent.SolutionThis error almost always results in a missing bracket somewhere.  Check your code carefully for missing brackets.  Using a PHP IDE can be useful for checking brackets that are not closed.  Brackets that span multiple PHP sections can be the trickiest to miss.  For instance:12345678910<?php$x = 3;if($x == 3){?> <a...