可以使用命令在任何一行停止执行python脚本吗?
喜欢
some code
quit() # quit at this point
some more code (that's not executed)
发布于 2009-02-12 21:20:20
sys.exit()将完全按照您的要求进行操作。
import sys
sys.exit("Error message")
发布于 2009-02-12 21:27:46
您可以使用raise SystemExit(0)
,而不是麻烦地使用import sys; sys.exit(0)
。
发布于 2009-02-13 05:22:08
你想要sys.exit()
。来自Python的文档:
>>> import sys
>>> print sys.exit.__doc__
exit([status])
Exit the interpreter by raising SystemExit(status).
If the status is omitted or None, it defaults to zero (i.e., success).
If the status is numeric, it will be used as the system exit status.
If it is another kind of object, it will be printed and the system
exit status will be one (i.e., failure).
所以,基本上,你会这样做:
from sys import exit
# Code!
exit(0) # Successful exit
https://stackoverflow.com/questions/543309
复制相似问题