sys.exit() 引发一个 SystemExit异常,若没有捕获这个异常,Python解释器会直接退出;捕获这个异常可以做一些额外的清理工作。0为正常退出,其他数值(1-127)为不正常,可抛异常事件供捕获。
exit() 跟 C 语言等其他语言的 exit() 应该是一样的。
如果是你手动调用这个函数就是要自己处理异常的情况。
一、功能不同
1、exit(0):表示程序正常退出。
2、exit(1):表示程序异常退出。
二、特点不同
1、exit(0):把exit用在main内的时候无论main是否定义成void返回的值都是有效的。
2、exit(1):等价于return(1),不需要考虑类型。
三、操作顺序不同
1、exit(0):直接使进程停止运行,清除其使用的内存空间,并销毁其在内核中的各种数据结构。
2、exit(1):在调用 exit 系统调用之前要检查文件的打开情况,把文件缓冲区中的内容写回文件。
参考资料来源:百度百科-Exit()
参考资料来源:百度百科-Exit
exit可能被site自定义文件改了,你打印一下exit的值,应该变成字符串了。需要咨询系统管理员。
正常的site文件exit是这样的:
$ pythonPython 2.7.10 (default, Aug 13 2015, 17:53:56)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit.__module__
'site'
>>> import site
>>> site.__file__
'/home/sylecn/opt/lib/python2.7/site.pyc'
>>> print exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> print type(exit)
<class 'site.Quitter'>
>>>
这是原版的site.py exit的定义:
def setquit():"""Define new builtins 'quit' and 'exit'.
These are objects which make the interpreter exit when called.
The repr of each object contains a hint at how it works.
"""
if os.sep == ':':
eof = 'Cmd-Q'
elif os.sep == '\\':
eof = 'Ctrl-Z plus Return'
else:
eof = 'Ctrl-D (i.e. EOF)'
class Quitter(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return 'Use %s() or %s to exit' % (self.name, eof)
def __call__(self, code=None):
# Shells like IDLE catch the SystemExit, but listen when their
# stdin wrapper is closed.
try:
sys.stdin.close()
except:
pass
raise SystemExit(code)
__builtin__.quit = Quitter('quit')
__builtin__.exit = Quitter('exit')