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 Connect to a Wireless Networking with Prompting for a Keyring Password in Ubuntu/Gnome

Sometimes the default installation of Ubuntu will continue to prompt for a keyring password in order to access the stored wifi password. This prevents automatic login to wifi networks. To remove this prompt for a keyring password and just automatically connect to the saved wireless network, do the following:Right click on the Network Manager icon in the system tray. Now, click Edit ConnectionsClick on the Wireless tab and select the connect you are using. Click the Edit buttonMake sure 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...

» How to Refurbish Your NES (so you do not have to blow on the games anymore)

The NES is one of the most successful systems to exist. Unfortunately, Nintendo didn't plan to have people still play the NES for this long. I'm here to tell you how to clean the games, get rid of that dreaded blinking screen, and how to clean your controller on the inside so it has the perfect response that it used to have. You dusted off your NES, hooked it up, ready to play Contra, turn it on, and you get the dreaded blinking red/blue/green/whatever color screen, depending on your model....