如何在Python 3.2中退出?

时间:2022-05-22 09:39:06

I know now we cant use sys.exit() . So how do I exit in the new version of Python?

我知道现在我们不能使用sys.exit()。那么如何在新版本的Python中退出?

1 个解决方案

#1


17  

import sys
sys.exit()

details from the sys module documentation:

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 consideredabnormal 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 sys.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.

退出Python。这是通过引发SystemExit异常来实现的,因此可以遵循try语句的finally子句指定的清理操作,并且可以拦截外层的退出尝试。可选参数arg可以是一个整数,给出退出状态(默认为零)或其他类型的对象。如果它是整数,则零被认为是成功终止'',并且任何非零值被贝壳等视为异常终止''。大多数系统要求它在0-127范围内,否则会产生不确定的结果。有些系统具有为特定退出代码指定特定含义的约定,但这些通常是不发达的; Unix程序通常使用2表示命令行语法错误,1表示所有其他类型的错误。如果传递了另一种类型的对象,则None等效于传递零,并且任何其他对象将打印到sys.stderr并导致退出代码为1.特别是,sys.exit(“某些错误消息”)是快速的发生错误时退出程序的方法。

Source

资源

#1


17  

import sys
sys.exit()

details from the sys module documentation:

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 consideredabnormal 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 sys.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.

退出Python。这是通过引发SystemExit异常来实现的,因此可以遵循try语句的finally子句指定的清理操作,并且可以拦截外层的退出尝试。可选参数arg可以是一个整数,给出退出状态(默认为零)或其他类型的对象。如果它是整数,则零被认为是成功终止'',并且任何非零值被贝壳等视为异常终止''。大多数系统要求它在0-127范围内,否则会产生不确定的结果。有些系统具有为特定退出代码指定特定含义的约定,但这些通常是不发达的; Unix程序通常使用2表示命令行语法错误,1表示所有其他类型的错误。如果传递了另一种类型的对象,则None等效于传递零,并且任何其他对象将打印到sys.stderr并导致退出代码为1.特别是,sys.exit(“某些错误消息”)是快速的发生错误时退出程序的方法。

Source

资源