首页 » 信息技术 »

Python的sys.exit()

2018年9月28日 / 131次阅读
Python

  • 打开支付宝,搜索“ 529018372 ”,领取专属红包!每日支付每日领。

Python程序的退出,一般都是使用sys.exit()函数。

sys.exit()会引发一个异常:SystemExit,如果这个异常没有被捕获,那么python解释器将会退出。如果有捕获此异常的代码,那么有些用于清理善后的代码会执行。捕获这个异常可以做一些额外的清理工作。0为正常退出,其他数值(1-127)为不正常,可抛异常事件供捕获。(成功退出时带参数0,似乎是一种惯例)

Python官方的解释:

sys.exit([arg])
Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.

The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0–127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.

Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.

Changed in version 3.6: If an error occurs in the cleanup after the Python interpreter has caught SystemExit (such as an error flushing buffered data in the standard streams), the exit status is changed to 120.

最后,麦新杰觉得,exit()和quit()这两个函数是一样的:

>>> help(exit)
Help on Quitter in module _sitebuiltins object:

class Quitter(builtins.object)
 |  Methods defined here:
 |
 |  __call__(self, code=None)
 |      Call self as a function.
 |
 |  __init__(self, name, eof)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  __repr__(self)
 |      Return repr(self).
 |
 |  -------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)

>>> help(quit)
Help on Quitter in module _sitebuiltins object:

class Quitter(builtins.object)
 |  Methods defined here:
 |
 |  __call__(self, code=None)
 |      Call self as a function.
 |
 |  __init__(self, name, eof)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  __repr__(self)
 |      Return repr(self).
 |
 |  -------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)

>>>

exit和quit的help信息完全一样。不管了,我们用sys.exit()就好了。

本文链接:http://www.maixj.net/ict/sys-exit-19007

相关文章

留言区

《Python的sys.exit()》有2条留言

电子邮件地址不会被公开。 必填项已用*标注

  • 麦新杰

    exit函数也可以给系统返回一个错误码,跟sys.exit()的区别,恐怕就是没有抛出一个可供利用的异常。

    (PayYourSlip) [xinlin@localhost PayYourSlip]$ python
    Python 3.7.1 (default, Oct 30 2018, 20:38:04)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.exit(100)
    (PayYourSlip) [xinlin@localhost PayYourSlip]$ echo $?
    100
    (PayYourSlip) [xinlin@localhost PayYourSlip]$ python
    Python 3.7.1 (default, Oct 30 2018, 20:38:04)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> exit(200)
    (PayYourSlip) [xinlin@localhost PayYourSlip]$ echo $?
    200
    
     [回复]

  • 麦新杰

    Linux系统进程成功执行完毕,给系统返回的都是0,这是惯例,也是文化。 [回复]


前一篇:
后一篇:
推一篇:可靠正规,长期稳定,网络兼职项目!!

栏目精选

云上小悟,麦新杰的独立博客

Ctrl+D 收藏本页

栏目


©Copyright 麦新杰 Since 2014 云上小悟独立博客版权所有 备案号:苏ICP备14045477号-1。云上小悟网站部分内容来源于网络,转载目的是为了整合信息,收藏学习,服务大家,有些转载内容也难以判断是否有侵权问题,如果侵犯了您的权益,请及时联系站长,我会立即删除。

网站二维码
go to top