One - One Code All

Blog Content

python程序退出和中断的方式

Python   2007-11-17 13:23:08

python的程序有两种退出方式:os._exit(), sys.exit()。

os._exit()会直接将python程序终止,之后的所有代码都不会继续执行。

sys.exit()会引发一个异常:SystemExit,如果这个异常没有被捕获,那么python解释器将会退出。如果有捕获此异常的代码,那么这些代码还是会执行。

一般情况下使用sys.exit()即可,一般在fork出来的子进程中使用os._exit()

import sys

try:
    sys.exit(0)
except:
    print 'die'
finally:
    print 'cleanup'



上一篇:java中for和foreach循环使用 ,for (int a : data)
下一篇:查看python安装路径

The minute you think of giving up, think of the reason why you held on so long.