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

How can I do this in Python?

解决方案import sys

sys.exit()

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

considered “abnormal 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 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.

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.

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.

python强行终止程序,如何终止Python脚本相关推荐

  1. python 客户端应用程序_创建python Web服务和C#客户端应用程序的最佳方法 - c#

    我正在寻找一种简单可靠的方法来创建Python Web Service并从.Net(c#)应用程序中使用它. 我发现了很多不同的库,其中一个库比另一个库好,但是似乎没有人提供关于Python Web ...

  2. python写机器人程序_用Python写的一个多线程机器人聊天程序

    本人是从事php开发的, 近来想通过php实现即时通讯(兼容windows).后来发现实现起来特别麻烦, 就想到python.听说这家伙在什么地方都能发挥作用.所以想用python来做通讯模块...所 ...

  3. python开发安卓程序-如何使用python开发android应用

    Python是一种面向对象.解释型计算机程序设计语言,其源代码和解释器CPython遵循GPL(GNU General Public License)协议Python语法简洁清晰,特色之一是强制用空白 ...

  4. python开发安卓程序-如何使用python开发Android手机应用?

    在使用python开发android应用之前我们需要准备好环境,环境需要安装PythonForAndroid,然后开始编程,编程时打开eclipse, 创建一个新的android project &q ...

  5. 30岁自学python找工作-程序员自学Python开发,20到30岁几乎决定了你的未来!

    原标题:程序员自学Python开发,20到30岁几乎决定了你的未来! 之前程序员界流行一句话:人生苦短,请用Python. 随着Python成为网红语言之后,不少程序员想多学这一门语言好傍身. 甚至有 ...

  6. java min 函数的使用方法_【Python】Java程序员学习Python(五)— 函数的定义和使用...

    不想做一个待宰的羔羊!!!!要自己变得强大.... 函数的定义和使用放在最前边还是有原因的,现在语言趋于通用,基本类型基本都是那些,重点还是学习对象的使用方法,而最根本的还是方法的使用,因此优先介绍, ...

  7. python二分法查找程序_查找Python程序的输出| 套装2(基础)

    python二分法查找程序 Program 1: 程序1: a = 10 b = 3 res = a/b print "a/b: ", res res = float(a/b) p ...

  8. 为什么一个程序中变量只能定义一次_#带你学Python# 从简单程序出发理解Python基本语法

    欢迎回来. 通过上一篇文章,我们第一次触摸了Python,学会了如何用各种不同的方式运行Python的解释器.也介绍了很多工具和开发环境,是不是跃跃欲试了? 到这里,别的python教程就会从数据类型 ...

  9. 用python写投票程序_大话python最终篇,web.py 开发的投票程序demo

    概述 开发语言         python Web开发框架  web.py 前端开发框架   vuejs+elementui 数据库              mysql 设计思路 首先是数据库设计 ...

  10. Python练习小程序 定时关机小脚本

    就不多介绍了,直接上代码吧. #coding:utf-8 import datetime, time, os, wx import tkinter, threading from tkinter im ...

最新文章

  1. LeetCode Add Two Numbers
  2. Qt Creator使用Bazaar集市
  3. 『数据库』数据库编程(概念性的东西,应用一般,甚至有点过时,用来考试)
  4. 2018CCPC吉林赛区(重现赛)补题部分——F线段树待补
  5. word 2013 题注、图注、插入图片自动修改大小、批量更新题注编号
  6. dedecms php5.4 无法退出后台,PHP5.4版本织梦dedecms后台退出空白的解决方法
  7. putty如何登陆oracle,利用putty的SSH tunnel连接Oracle
  8. ad15的stc元件库_altium designer常用元件库和封装库下载-非常全面初学者有福了
  9. 疯狂软件Oracle数据库视频
  10. FFmpeg —— 录制windows系统声音(附源码)
  11. Android那些你可能没了解过的---碎片化
  12. 什么是搜索引擎优化及其重要性
  13. jQuery 效果 - stop() 方法
  14. Unity 智能语音助手
  15. MYSQL数据库误删除恢复笔记收藏
  16. sumo设置公交车站_城市交通仿真平台SUMO.pdf
  17. 微软控诉四大PC品牌经销商
  18. hp外带检测服务器硬件,HP Gen10 MicroServer 篇一:#原创新人#惠普 HP Gen10 MicroServer 家用服务器 开箱...
  19. 计算机常用的加密方法,加密方式有哪些_教你常用加密方式有哪些
  20. 服务器信息泄露,Windows2008 r2“Web服务器HTTP头信息泄露”漏洞修复

热门文章

  1. 与MyBatis的第一次见面课
  2. MySQL查询日期类数据常用函数
  3. React:Redux简介
  4. SQL:pgsql中查询一段时间内数据及相关时间操作
  5. JCTools简介-增强的并发工具
  6. Maven打jar发布包的常用配置
  7. LeetCode之移除元素
  8. hyperledger fabric超级账本java sdk样例e2e代码流程分析
  9. 设置pyspark 启动时所使用的python版本
  10. JS执行一次任务与定期任务与清除执行