首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >以编程方式停止执行python脚本?

以编程方式停止执行python脚本?
EN

Stack Overflow用户
提问于 2009-02-13 05:18:06
回答 4查看 647.3K关注 0票数 257

可以使用命令在任何一行停止执行python脚本吗?

喜欢

代码语言:javascript
复制
some code

quit() # quit at this point

some more code (that's not executed)
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-02-12 21:20:20

sys.exit()将完全按照您的要求进行操作。

代码语言:javascript
复制
import sys
sys.exit("Error message")
票数 410
EN

Stack Overflow用户

发布于 2009-02-12 21:27:46

您可以使用raise SystemExit(0),而不是麻烦地使用import sys; sys.exit(0)

票数 159
EN

Stack Overflow用户

发布于 2009-02-13 05:22:08

你想要sys.exit()。来自Python的文档:

代码语言:javascript
复制
>>> 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).

所以,基本上,你会这样做:

代码语言:javascript
复制
from sys import exit

# Code!

exit(0) # Successful exit
票数 37
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/543309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档