Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

python - 终止Python脚本(Terminating a Python script)

I am aware of the die() command in PHP which stops a script early.

(我知道PHP中的die()命令可尽早停止脚本。)

How can I do this in Python?

(如何在Python中执行此操作?)

  ask by Teifion translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
import sys
sys.exit()

details from the sys module documentation :

(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等于传递零,并且将任何其他对象stderrstderr并导致退出代码为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()最终“仅”引发异常,因此它仅在从主线程调用时才退出进程,并且不会拦截该异常。)

Note that this is the 'nice' way to exit.

(请注意,这是退出的“不错”方式。)

@ glyphtwistedmatrix below points out that if you want a 'hard exit', you can use os._exit( errorcode ), though it's likely os-specific to some extent (it might not take an errorcode under windows, for example), and it definitely is less friendly since it doesn't let the interpreter do any cleanup before the process dies.

(@ glyphtwistedmatrix低于指出,如果你想要一个“硬退出”,你可以使用os._exit( 错误代码 ),但它很可能与操作系统相关的在一定程度上(也许它不会在Windows下一个错误代码,例如),它绝对不是那么友好,因为它不允许解释器在过程终止之前进行任何清理。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...