1》sys.exit() 
>>> import sys
>>> help(sys.exit)
Help on built-in function exit in module sys:
exit(...)
    exit([status])
    
    Exit the interpreter by raising SystemExit(status).
    If the status is omitted or None, it defaults to zero (i.e., success).
    If the status is an integer, it will be used as the system exit status.
    If it is another kind of object, it will be printed and the system
    exit status will be one (i.e., failure).

执行该语句会直接退出程序,这也是经常使用的方法,也不需要考虑平台等因素的影响,一般是退出Python程序的首选方法。
退出程序引发SystemExit异常,(这是唯一一个不会被认为是错误的异常), 如果没有捕获这个异常将会直接退出程序执行,
当然也可以捕获这个异常进行一些其他操作(比如清理工作)。
sys.exit()函数是通过抛出异常的方式来终止进程的,也就是说如果它抛出来的异常被捕捉到了的话程序就不会退出了,
而是去进行一些清理工作。
SystemExit 并不派生自Exception 所以用Exception捕捉不到该SystemEixt异常,应该使用SystemExit来捕捉。
该方法中包含一个参数status,默认为0,表示正常退出, 其他都是异常退出。
还可以这样使用:sys.exit("Goodbye!"); 一般主程序中使用此退出.

捕获到SystemExit异常,程序没有直接退出!
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
    try:
        sys.exit(825)
    except SystemExit,error:
        print 'the information of SystemExit:{0}'.format(error)
        print "the program doesn't exit!"
    print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
the information of SystemExit:825
the program doesn't exit!
Now,the game is over!
song@ubuntu:~$

没有捕获到SystemExit异常,程序直接退出,后边的代码不执行!
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
    try:
        sys.exit(825)
    except Exception,error:
        print 'the information of SystemExit:{0}'.format(error)
        print "the program doesn't exit!"
    print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
song@ubuntu:~$

没有捕获到SystemExit异常,输出'Goodbye!'后,程序直接退出,后边的代码不执行!
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
    try:
        sys.exit('Goodbye!')
    except Exception,error:
        print 'the information of SystemExit:{0}'.format(error)
        print "the program doesn't exit!"
    print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
Goodbye!
song@ubuntu:~$

2》os._exit(), 直接退出 Python 解释器, 不抛异常, 不执行相关清理工作,其后的代码都不执行,
其使用会受到平台的限制,但我们常用的Win32平台和基于UNIX的平台不会有所影响, 常用在子进程的退出.
一般来说os._exit() 用于在线程中退出,sys.exit() 用于在主线程中退出。

3》exit()/quit(), 抛出SystemExit异常. 一般在交互式shell中退出时使用.

python:sys.exit() os._exit() exit() quit()的更多相关文章

  1. python之 sys.exit() os._exit() exit() quit()的简单使用

    python之sys.exit() os._exit() exit() quit()的简单使用 1>sys.exit() >>> import sys>>> ...

  2. Python sys模块 os模块、OS.open() | open() | OS._exit() | sys.exit() | exit()

    sys模块:负责程序和Python交互. sys常用方法:===========================  sys.stdout.write('please:')val = sys.stdin ...

  3. Python程序退出方式(sys.exit() os._exit() os.kill() os.popen(...))

    对于如何结束一个Python程序或者用Python操作去结束一个进程等,Python本身给出了好几种方法,而这些方式也存在着一些区别,对相关的几种方法看了并实践了下,同时也记录下. 参考: Pytho ...

  4. python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别

    os._exit() 和 sys.exit() os._exit() vs sys.exit() 概述 Python的程序有两中退出方式:os._exit(), sys.exit().本文介绍这两种方 ...

  5. python 之 os._exit() sys.exit() 、exit()

    sys.exit 执行该语句会直接退出程序,这也是经常使用的方法,也不需要考虑平台等因素的影响,一般是退出Python程序的首选方法. 退出程序引发SystemExit异常,(这是唯一一个不会被认为是 ...

  6. Python os._exit() sys.exit()

    os._exit()会直接将python程序终止,之后的所有代码都不会继续执行. sys.exit()会引发一个异常:SystemExit,如果这个异常没有被捕获,那么python解释器将会退出.如果 ...

  7. Python中 os._exit() sys.exit() exit()区别

    Python退出程序的方式有两种:os._exit(), sys.exit() 1)os._exit() 直接退出 Python程序,其后的代码也不会继续执行. 2)sys.exit() 引发一个 S ...

  8. python基础之 Python os._exit() sys.exit() exit()区别

    Python退出程序的方式有两种:os._exit(), sys.exit() 1)os._exit() 直接退出 Python程序,其后的代码也不会继续执行. 2)sys.exit() 引发一个 S ...

  9. python中sys.exit()和os._exit(0)退出程序

    python中退出程序的两种方法,0为默认状态,可以为空,两者均会退出当前运行的程序,os._exit(0)中的0不能省略 sys.exit(0):可以捕获SystemExit异常,然后做相应的清理工 ...

随机推荐

  1. easy-ui 中的事件触发 (tree)

    easy-ui可以为插件添加事件,但没有触发事件的处理(可能是未找到),所以有时候,我们需要通过程序去触发某个插件指定的事件时,就一筹莫展了 以Tree插件为例 ,添加了onClick事件 jQuer ...

  2. XVIII Open Cup named after E.V. Pankratiev. Eastern Grand Prix

    A. Artifacts 建立语法分析树,首先根据上下界判断是否有解,然后将所有数按下界填充,线段树判断是否存在和超过$K$的子区间. B. Brackets and Dots 最优解中一定包含一对中 ...

  3. MySQL性能分析及explain的使用(转)

    1.使用explain语句去查看分析结果,如 explain select * from test1 where id=1; 会出现: id selecttype table type possibl ...

  4. Tips_利用padding实现高度可控的分隔线

    一.实现分隔线的方法(未理解:不是说span元素垂直方向设置怕padding不影响吗?) html: <div> 登陆<span></span>注册 </di ...

  5. Codechef July Challenge 2018 : Subway Ride

    传送门 首先(想了很久之后)注意到一个性质:同一条边有多种颜色的话保留3种就可以了,这是因为假如最优解要求当前位置与相邻两条边都不相同,那么只要有3条边,就肯定可以满足这一点. 完事就做一个nlogn ...

  6. STL--set_difference

    set_difference(),作用是求两个集合的差.即求A-B(属于A但不属于B的元素) set_difference()算法计算两个集合[start1, end1)和[start2, end2) ...

  7. Wireshark简单使用教程3——附视频

    视频https://www.bilibili.com/video/av35763613?from=search&seid=10176480091153063668 目录 抓取干净流量包的用处所 ...

  8. Bypass 360主机卫士SQL注入防御(附tamper脚本)

    0x01 前言 在测试过程中,经常会遇到一些主机防护软件,对这方面做了一些尝试,可成功bypass了GET和POST的注入防御,分享一下姿势. 0x02 环境搭建 Windows Server 200 ...

  9. jquery 实现tab切换

    大家都知道 使用QQ的时候需要输入账号和密码 这个时候一个TAB键盘就可以实现切换到下一个输入框里 具体是怎么实现的呢 请看代码 <!DOCTYPE html> <html lang ...

  10. 2018-2019-2 20175320实验二《Java面向对象程序设计》实验报告

    2018-2019-2 20175320实验二<Java面向对象程序设计>实验报告 一.实验步骤及内容 (一)了解使用JUint,并对示例代码MyUtil进行测试 1.先在IDEA中安装J ...