如何在try/exep中停止程序

2024-03-28 13:54:25 发布

您现在位置:Python中文网/ 问答频道 /正文

当我的try循环得到尊重时,我想停止我的程序

我试着把系统出口()语句,但程序不停止

def prints():
    with open ("{}.txt".format(name), "r") as printer :
    contain = printer.readlines()
    print(''.join(contain))

name = input("Enter the file name : ")
try:
    prints()
    sys.exit()
except:
    pass

<more code>

我本来希望程序在运行prints函数后会停止,但现在程序在运行函数后继续运行


Tags: 函数name程序txtformat系统defas
1条回答
网友
1楼 · 发布于 2024-03-28 13:54:25

except捕获SystemExit,因此sys.exit()调用被阻止导致程序退出。你知道吗

sys.exit()移出try。你知道吗

我也不想看到一个没有except的空的logging.exception('')。你知道吗

相关问题 更多 >