How to stop execution in python script?

Possible duplicates:
Programmatically stop python script execution?
Python shutdown script

I want to print the value and then stop the execution of the script.

Am I just using return?

+7
python
source share
3 answers

You can use the return inside the main function in which it exists, but this does not guarantee exit from the script if more code appears after your call to the main one.

The simplest one that almost always works is sys.exit() :

 import sys sys.exit() 

Other features:

  • Raise an error that was not detected.
  • Let the execution point reach the end of the script.
  • If you are using a thread other than the main thread, use thread.interrupt_main() .
+17
source share

The exit function in the sys ( docs ) module:

 import sys sys.exit( 0 ) # 0 will be passed to OS 

you also can

 raise SystemExit 

or any other exception that will not be detected.

+7
source share
+6
source share

All Articles