[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
一
sys.exit()
执行这条语句会直接退出程序,也是常用的方法,不需要考虑平台等因素的影响。它通常是退出 Python 程序的首选方法。
该方法包含一个参数status,默认为0,表示正常退出,或1,表示异常退出。
import sys sys.exit() # 退出当前程序,但不重启shell sys.exit(0) sys.exit(1)
此方法引发 SystemExit
异常(这是唯一不被视为错误的异常)。当这个异常没有设置为捕获时,会直接退出程序执行。当然,也可以捕获这个异常来执行一些其他的操作。
os._exit()
效果也是直接退出而不抛出异常,但是它的使用会受到平台的限制,但是我们常用的Win32平台和基于UNIX的平台不会受到影响。
os.kill()
一般用于直接Kill掉进程,但是只能在UNIX平台上有效。
基本原理:该函数模拟传统的UNIX函数向进程发送信号,其中包含两个参数:一个是进程名,即接收信号的进程;另一个是要执行的操作。
操作(第二个参数)的常用取值为:
SIGINT 终止进程 中断进程 SIGTERM 终止进程 软件终止信号 SIGKILL 终止进程 杀死进程 SIGALRM 闹钟信号
二
exit()退出程序,并重启shell
exit() # 退出当前程序,并重启shell
quit()退出程序,并重启shell,效果与exit()一样
quit() # 与exit()效果一样,退出并重启shell
三 快捷键
Ctrl + C
如果是程序陷入死循环,想强制结束,则按Ctrl + C。这个特别关键。
注意:捕获所有异常可能会造成意外的结果,比如,通常我们使用CTRL+C来终止程序,但如果程序中捕获了所有异常,CTRL+C就无法终止程序了。
<>今天的分享就到此结束了
欢迎点赞评论互关三连