I am aware of the die() command in PHP which stops a script early.
die()
(我知道PHP中的die()命令可尽早停止脚本。)
How can I do this in Python?
(如何在Python中执行此操作?)
import sys sys.exit()
details from the sys module documentation :
sys
(sys模块文档中的详细信息:)
sys. exit ([ arg ]) Exit from Python. (从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. (这是通过引发SystemExit异常来实现的,因此可以使用try语句的finally子句指定的清除操作,并且可以在外部级别拦截出口尝试。) The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. (可选参数arg可以是给出退出状态的整数(默认为零),也可以是其他类型的对象。)If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. (如果是整数,则shell等将零视为“成功终止”,而将任何非零值视为“异常终止”。)Most systems require it to be in the range 0-127, and produce undefined results otherwise. (大多数系统要求它的范围是0-127,否则会产生不确定的结果。)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. (Unix程序通常将2用于命令行语法错误,将1用于所有其他类型的错误。)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. (如果传递了另一种类型的对象,则None等于传递零,并且将任何其他对象stderr到stderr并导致退出代码为1。特别是, sys.exit("some error message")是一种快速的方法发生错误时退出程序。) 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. (由于exit()最终“仅”引发异常,因此它仅在从主线程调用时才退出进程,并且不会拦截该异常。)
sys. exit ([ arg ])
Exit from Python. (从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. (这是通过引发SystemExit异常来实现的,因此可以使用try语句的finally子句指定的清除操作,并且可以在外部级别拦截出口尝试。) The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. (可选参数arg可以是给出退出状态的整数(默认为零),也可以是其他类型的对象。)If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. (如果是整数,则shell等将零视为“成功终止”,而将任何非零值视为“异常终止”。)Most systems require it to be in the range 0-127, and produce undefined results otherwise. (大多数系统要求它的范围是0-127,否则会产生不确定的结果。)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. (Unix程序通常将2用于命令行语法错误,将1用于所有其他类型的错误。)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. (如果传递了另一种类型的对象,则None等于传递零,并且将任何其他对象stderr到stderr并导致退出代码为1。特别是, sys.exit("some error message")是一种快速的方法发生错误时退出程序。) 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. (由于exit()最终“仅”引发异常,因此它仅在从主线程调用时才退出进程,并且不会拦截该异常。)
Exit from Python.
(从Python退出。)
SystemExit
try
(这是通过引发SystemExit异常来实现的,因此可以使用try语句的finally子句指定的清除操作,并且可以在外部级别拦截出口尝试。)
The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object.
(可选参数arg可以是给出退出状态的整数(默认为零),也可以是其他类型的对象。)
(如果是整数,则shell等将零视为“成功终止”,而将任何非零值视为“异常终止”。)
(大多数系统要求它的范围是0-127,否则会产生不确定的结果。)
(某些系统具有为特定的退出代码分配特定含义的约定,但是这些通常不完善。)
(Unix程序通常将2用于命令行语法错误,将1用于所有其他类型的错误。)
stderr
sys.exit("some error message")
(如果传递了另一种类型的对象,则None等于传递零,并且将任何其他对象stderr到stderr并导致退出代码为1。特别是, sys.exit("some error message")是一种快速的方法发生错误时退出程序。)
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.
exit()
(由于exit()最终“仅”引发异常,因此它仅在从主线程调用时才退出进程,并且不会拦截该异常。)
Note that this is the 'nice' way to exit.
(请注意,这是退出的“不错”方式。)
(@ glyphtwistedmatrix低于指出,如果你想要一个“硬退出”,你可以使用os._exit( 错误代码 ),但它很可能与操作系统相关的在一定程度上(也许它不会在Windows下一个错误代码,例如),它绝对不是那么友好,因为它不允许解释器在过程终止之前进行任何清理。)
2.1m questions
2.1m answers
60 comments
57.0k users