python:sys.exit() os._exit() exit() quit()
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()的更多相关文章
- python之 sys.exit() os._exit() exit() quit()的简单使用
python之sys.exit() os._exit() exit() quit()的简单使用 1>sys.exit() >>> import sys>>> ...
- Python sys模块 os模块、OS.open() | open() | OS._exit() | sys.exit() | exit()
sys模块:负责程序和Python交互. sys常用方法:=========================== sys.stdout.write('please:')val = sys.stdin ...
- Python程序退出方式(sys.exit() os._exit() os.kill() os.popen(...))
对于如何结束一个Python程序或者用Python操作去结束一个进程等,Python本身给出了好几种方法,而这些方式也存在着一些区别,对相关的几种方法看了并实践了下,同时也记录下. 参考: Pytho ...
- python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别
os._exit() 和 sys.exit() os._exit() vs sys.exit() 概述 Python的程序有两中退出方式:os._exit(), sys.exit().本文介绍这两种方 ...
- python 之 os._exit() sys.exit() 、exit()
sys.exit 执行该语句会直接退出程序,这也是经常使用的方法,也不需要考虑平台等因素的影响,一般是退出Python程序的首选方法. 退出程序引发SystemExit异常,(这是唯一一个不会被认为是 ...
- Python os._exit() sys.exit()
os._exit()会直接将python程序终止,之后的所有代码都不会继续执行. sys.exit()会引发一个异常:SystemExit,如果这个异常没有被捕获,那么python解释器将会退出.如果 ...
- Python中 os._exit() sys.exit() exit()区别
Python退出程序的方式有两种:os._exit(), sys.exit() 1)os._exit() 直接退出 Python程序,其后的代码也不会继续执行. 2)sys.exit() 引发一个 S ...
- python基础之 Python os._exit() sys.exit() exit()区别
Python退出程序的方式有两种:os._exit(), sys.exit() 1)os._exit() 直接退出 Python程序,其后的代码也不会继续执行. 2)sys.exit() 引发一个 S ...
- python中sys.exit()和os._exit(0)退出程序
python中退出程序的两种方法,0为默认状态,可以为空,两者均会退出当前运行的程序,os._exit(0)中的0不能省略 sys.exit(0):可以捕获SystemExit异常,然后做相应的清理工 ...
随机推荐
- 图片编辑工具GIMP
今天修改图片: 给图片添加alpha通道,选中要删去的部分,就会变成透明,要保存为png格式 文库参考: http://wenku.baidu.com/link?url=HR1lKoBKS1xbhUJ ...
- vue_v-for_遍历数组_遍历对象
1. v-for 遍历数组 html <div id="test"> <ul> <li v-for="(p, index) in perso ...
- Container/Injection
1.容器的历史 容器概念始于 1979 年提出的 UNIX chroot,它是一个 UNIX 操作系统的系统调用,将一个进程及其子进程的根目录改变到文件系统中的一个新位置,让这些进程只能访问到这个新的 ...
- bulid tools
输入:工程文件+编译说明文件: 处理:自动化构建工具+编译器: 输出:可执行文件. 相对于手动编译. 概述历史上 , 并通过构建自动化Makefile.今天 , 有两种一般类型的工具 : 自动工具 ( ...
- Oracle 11g修改字符集
选择静默安装的安装字符集为默认的ZHS16GBK,工作中字符集为为AL32UTF8 一.登录oracle sqlplus / as sysdba shutdown immediate; STARTUP ...
- Asch PK Lisk系列之一:安全性
在币圈,听到对数字货币的质疑之声从来没少过.为什么有人会质疑呢?他们列出了很多理由(以下四点内容摘自网络): 数字货币是依附于网络的,而中国并没有独立自主的网络技术,容易被敌对势力利用数字货币损害中国 ...
- sql server里中自增长的ID重新开始排
dbcc checkident('tablename',reseed,0); 执行:dbcc checkident('TableA',reseed,0); 执行结束:中途报了几次插入重复键. 结论:用 ...
- 小甲鱼零基础python课后题 P22 021函数:递归是神马
0.递归在编程上的形式是如何表现的呢? 答:在编程上,递归表现为函数调用本身这么一个行为. 1.递归必须满足哪两个基本条件? 答:1函数调用自己. 2有正确的返回条件 2.思考一下,按照递归的特性,在 ...
- C++标准库algorithm
(1) 基本数学相关: max(t1, t2)和min(t1, t2), 返回t1和t2中的较大.较小者. max_element(b, e)和min_element(b, e), 返回两个迭代器所指 ...
- LeetCode 240 - 搜索二维矩阵 II
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列.每列的元素从上到下升序排列.示例: 现有矩阵 matrix 如 ...