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中format()方法的使用,参考:python format()方法

(完)

python之 sys.exit() os._exit() exit() quit()的简单使用的更多相关文章

  1. python:sys.exit() os._exit() exit() quit()

    1>sys.exit() >>> import sys>>> help(sys.exit)Help on built-in function exit in ...

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

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

  3. python中sys和os模块的使用

    在python中,sys,os模块是非常强大的,提供了许多对文件夹.文件和路径的操作方法 sys模块 sys.argv   #命令行执行脚本,其实它就是一个列表 ,sys.argv[0] 是程序自身路 ...

  4. Python中sys和os模块的区别

    sys: This module provides access to some variables used or maintained by the interpreter and to func ...

  5. 【Python】 sys和os模块

    sys sys模块能使程序访问于python解释器联系紧密的变量和函数 ● sys中的一些函数和变量 argv 命令行参数构成的列表 path 查找所有可用模块所在的目录名的列表 platform 查 ...

  6. python的sys和os模块

    一.sys sys.argv:实现从程序外部向程序传递参数.  其中sys.argv[0]为脚本的名称,所以要判断是否有参数传入可以:if len(sys.argv) > 1.  sys.exi ...

  7. python中sys和os的区别

    <os和sys的官方解释> ➤os os: This module provides a portable way of using operating system dependent ...

  8. python基础—sys与os库

    python可以用sys库打印环境变量或者查看当前文件的脚本路径,具体代码: import sysprint(sys.path[2])#打印环境变量print(sys.argv)#当前脚本路径 os库 ...

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

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

随机推荐

  1. block 和delegate的用法

    //block 和delegate的用法 设置代理 #import <UIKit/UIKit.h> typedef void (^ASIHeadersBlock)(NSString *my ...

  2. bzoj3141: [Hnoi2013]旅行

    Description   Input 第 一行为两个空格隔开的正整数n, m,表示旅行的城市数与旅行所花的月数.接下来n行,其中第 i行包含两个空格隔开的整数Ai和Bi,Ai表示他第i个去的城市编号 ...

  3. 关于Java(标识符规则)

    Java 是大小写敏感的语言. Java 标识符组成 Java 标识符组成: 字母,数字,下划线和美元符 $ Java 标识符规则 仅包含 字母,数字,下划线和美元符 开头不能使数字 不能使 Java ...

  4. BZOJ 3872 Ant colony

    Description There is an entrance to the ant hill in every chamber with only one corridor leading int ...

  5. 教你在Java的普通类中轻松获取Session以及request中保存的值

    曾经有多少人因为不知如何在业务类中获取自己在Action或页面上保存在Session中值,当然也包括我,但是本人已经学到一种办法可以解决这个问题,来分享下,希望对你有多多少少的帮助! 如何在Java的 ...

  6. 【Java】Java Socket编程(1)基本的术语和概念

    计算机程序能够相互联网,相互通讯,这使一切都成为可能,这也是当今互联网存在的基础.那么程序是如何通过网络相互通信的呢?这就是我记录这系列的笔记的原因.Java语言从一开始就是为了互联网而设计的,它为实 ...

  7. C++ Primer 随笔 Chapter 9 顺序容器

     参考:http://www.cnblogs.com/kurtwang/archive/2010/08/19/1802912.html 1..顺序容器:vector(快速随机访问):list(快速插入 ...

  8. P2032 「Poetize9」升降梯上

    描述 开启了升降梯的动力之后,探险队员们进入了升降梯运行的那条竖直的隧道,映入眼帘的是一条直通塔顶的轨道.一辆停在轨道底部的电梯.和电梯内一杆控制电梯升降的巨大手柄.Nescafe之塔一共有N层,升降 ...

  9. 数学(线性规划): ZJOI2013 防守战线

    偷懒用的线性规划. #include <iostream> #include <cstring> #include <cstdio> using namespace ...

  10. Mac下Shell快捷键

    ctrl+a //移到行首 ctrl+e //移到行尾 ctrl+y // 插入最近删除的单词或语句 ctrl+k //删除光标处到行尾部分 ctrl+u //删除光标处到行首部分 ctrl+w // ...