{"id":1018640,"date":"2024-12-27T12:44:35","date_gmt":"2024-12-27T04:44:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1018640.html"},"modified":"2024-12-27T12:44:36","modified_gmt":"2024-12-27T04:44:36","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e7%a8%8b%e5%ba%8f%e5%a4%b1%e6%95%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1018640.html","title":{"rendered":"Python\u5982\u4f55\u8ba9\u7a0b\u5e8f\u5931\u6548"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161241\/7f46ca4b-584f-4a90-a9e0-3a121093c22e.webp\" alt=\"Python\u5982\u4f55\u8ba9\u7a0b\u5e8f\u5931\u6548\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8ba9\u7a0b\u5e8f\u5931\u6548\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<strong>\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u673a\u5236\u6355\u83b7\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u3001\u4f7f\u7528<code>sys.exit()<\/code>\u51fd\u6570\u4e3b\u52a8\u9000\u51fa\u7a0b\u5e8f\u3001\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u903b\u8f91\u63a7\u5236\u7a0b\u5e8f\u7684\u6267\u884c\u3001\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u673a\u5236\u505c\u6b62\u7a0b\u5e8f<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u5f02\u5e38\u5904\u7406\u673a\u5236<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u68c0\u6d4b\u9519\u8bef\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u4ece\u800c\u4f18\u96c5\u5730\u505c\u6b62\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002<\/p>\n<\/p>\n<p><p>\u5f02\u5e38\u5904\u7406\u673a\u5236\u5728Python\u4e2d\u901a\u8fc7<code>try-except<\/code>\u7ed3\u6784\u5b9e\u73b0\u3002\u4f7f\u7528\u6b64\u673a\u5236\u65f6\uff0c\u53ef\u4ee5\u5c06\u53ef\u80fd\u53d1\u751f\u9519\u8bef\u7684\u4ee3\u7801\u653e\u5728<code>try<\/code>\u5757\u4e2d\uff0c\u7136\u540e\u5728<code>except<\/code>\u5757\u4e2d\u6355\u83b7\u7279\u5b9a\u7684\u5f02\u5e38\u7c7b\u578b\u5e76\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u6837\uff0c\u5f53\u7a0b\u5e8f\u5728<code>try<\/code>\u5757\u4e2d\u9047\u5230\u9519\u8bef\u65f6\uff0c\u53ef\u4ee5\u6355\u83b7\u9519\u8bef\u5e76\u51b3\u5b9a\u5982\u4f55\u5904\u7406\uff0c\u800c\u4e0d\u662f\u8ba9\u7a0b\u5e8f\u76f4\u63a5\u5d29\u6e83\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5931\u6548\uff0c\u8fd8\u53ef\u4ee5\u5728\u5931\u6548\u65f6\u63d0\u4f9b\u9002\u5f53\u7684\u4fe1\u606f\u6216\u65e5\u5fd7\u8bb0\u5f55\uff0c\u4ece\u800c\u5e2e\u52a9\u540e\u7eed\u7684\u9519\u8bef\u4fee\u590d\u548c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u5f02\u5e38\u5904\u7406\u673a\u5236<\/p>\n<\/p>\n<p><p>\u5f02\u5e38\u5904\u7406\u662fPython\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u673a\u5236\uff0c\u5b83\u4f7f\u5f97\u7a0b\u5e8f\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u68c0\u6d4b\u5230\u5f02\u5e38\u60c5\u51b5\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u4ece\u800c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5f02\u5e38\u5904\u7406\u529f\u80fd\uff0c\u5305\u62ec\u591a\u79cd\u5185\u7f6e\u5f02\u5e38\u7c7b\u578b\u3001\u7075\u6d3b\u7684\u5f02\u5e38\u6355\u83b7\u548c\u81ea\u5b9a\u4e49\u5f02\u5e38\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5f02\u5e38\u7c7b\u578b<\/li>\n<\/ol>\n<p><p>Python\u6709\u8bb8\u591a\u5185\u7f6e\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u5982<code>ValueError<\/code>\u3001<code>TypeError<\/code>\u3001<code>IndexError<\/code>\u7b49\u3002\u6bcf\u79cd\u5f02\u5e38\u7c7b\u578b\u90fd\u5bf9\u5e94\u7279\u5b9a\u7684\u9519\u8bef\u60c5\u51b5\u3002\u4f8b\u5982\uff0c<code>ValueError<\/code>\u901a\u5e38\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u6b63\u786e\u4f46\u503c\u975e\u6cd5\u65f6\u88ab\u629b\u51fa\uff0c\u800c<code>TypeError<\/code>\u5219\u5728\u64cd\u4f5c\u6216\u51fd\u6570\u5e94\u7528\u4e8e\u4e0d\u9002\u5f53\u7684\u7c7b\u578b\u65f6\u88ab\u629b\u51fa\u3002\u6b63\u786e\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u5f02\u5e38\u7c7b\u578b\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u6709\u6548\u5904\u7406\u4e0d\u540c\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><code>try-except<\/code>\u7ed3\u6784<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u53ef\u4ee5\u6355\u83b7\u7a0b\u5e8f\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u5e76\u901a\u8fc7<code>except<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u6355\u83b7\u4e86\u4e00\u4e2a<code>ValueError<\/code>\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    number = int(input(&quot;Enter a number: &quot;))<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;That&#39;s not a valid number!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7<code>ValueError<\/code>\u5f02\u5e38\u5e76\u6253\u5370\u9519\u8bef\u4fe1\u606f\uff0c\u800c\u4e0d\u662f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u591a\u4e2a\u5f02\u5e38\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u5728<code>try-except<\/code>\u7ed3\u6784\u4e2d\uff0c\u53ef\u4ee5\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    number = int(input(&quot;Enter a number: &quot;))<\/p>\n<p>    result = 10 \/ number<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;That&#39;s not a valid number!&quot;)<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    print(&quot;You can&#39;t divide by zero!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4e0d\u4ec5\u6355\u83b7<code>ValueError<\/code>\u5f02\u5e38\uff0c\u8fd8\u6355\u83b7<code>ZeroDivisionError<\/code>\u5f02\u5e38\u3002\u5982\u679c\u7528\u6237\u8f93\u51650\uff0c\u4f1a\u5f15\u53d1<code>ZeroDivisionError<\/code>\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7\u5e76\u6253\u5370\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol start=\"4\">\n<li><code>else<\/code>\u548c<code>finally<\/code>\u5b50\u53e5<\/li>\n<\/ol>\n<p><p>\u9664\u4e86<code>except<\/code>\u5b50\u53e5\uff0c<code>try<\/code>\u7ed3\u6784\u8fd8\u53ef\u4ee5\u5305\u62ec<code>else<\/code>\u548c<code>finally<\/code>\u5b50\u53e5\u3002<code>else<\/code>\u5b50\u53e5\u4e2d\u7684\u4ee3\u7801\u4ec5\u5728<code>try<\/code>\u5757\u6ca1\u6709\u5f15\u53d1\u5f02\u5e38\u65f6\u6267\u884c\uff0c\u800c<code>finally<\/code>\u5b50\u53e5\u4e2d\u7684\u4ee3\u7801\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u4f1a\u6267\u884c\u3002<code>finally<\/code>\u5b50\u53e5\u901a\u5e38\u7528\u4e8e\u91ca\u653e\u8d44\u6e90\uff0c\u5982\u5173\u95ed\u6587\u4ef6\u6216\u7f51\u7edc\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>    content = file.read()<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;File not found!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;File content:&quot;, content)<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u65e0\u8bba\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u7a0b\u5e8f\u90fd\u4f1a\u5c1d\u8bd5\u5173\u95ed\u6587\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u8d44\u6e90\u88ab\u6b63\u786e\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>sys.exit()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>sys.exit()<\/code>\u51fd\u6570\u662fPython\u6807\u51c6\u5e93<code>sys<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u7528\u4e8e\u9000\u51fa\u7a0b\u5e8f\u3002\u8c03\u7528<code>sys.exit()<\/code>\u65f6\uff0c\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u6574\u6570\u53c2\u6570\u4f5c\u4e3a\u9000\u51fa\u72b6\u6001\u7801\uff0c0\u901a\u5e38\u8868\u793a\u6b63\u5e38\u9000\u51fa\uff0c\u800c\u975e\u96f6\u503c\u8868\u793a\u5f02\u5e38\u9000\u51fa\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>\u8981\u4f7f\u7528<code>sys.exit()<\/code>\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165<code>sys<\/code>\u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;This is a test program.&quot;)<\/p>\n<p>sys.exit(0)<\/p>\n<p>print(&quot;This line will not be executed.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5728\u8c03\u7528<code>sys.exit(0)<\/code>\u540e\u7acb\u5373\u9000\u51fa\uff0c\u56e0\u6b64\u540e\u9762\u7684\u6253\u5370\u8bed\u53e5\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u9000\u51fa\u72b6\u6001\u7801<\/li>\n<\/ol>\n<p><p>\u4f20\u9012\u7ed9<code>sys.exit()<\/code>\u7684\u53c2\u6570\u901a\u5e38\u7528\u4e8e\u6307\u793a\u7a0b\u5e8f\u7684\u9000\u51fa\u72b6\u6001\u30020\u8868\u793a\u7a0b\u5e8f\u6b63\u5e38\u9000\u51fa\uff0c\u800c\u5176\u4ed6\u6574\u6570\u503c\u53ef\u4ee5\u7528\u4e8e\u6307\u793a\u4e0d\u540c\u7684\u9519\u8bef\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def divide(a, b):<\/p>\n<p>    if b == 0:<\/p>\n<p>        print(&quot;Error: Division by zero!&quot;)<\/p>\n<p>        sys.exit(1)  # \u9000\u51fa\u72b6\u6001\u7801\u4e3a1<\/p>\n<p>    return a \/ b<\/p>\n<p>result = divide(10, 0)<\/p>\n<p>print(&quot;Result:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7528\u6237\u5c1d\u8bd5\u9664\u4ee5\u96f6\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370\u9519\u8bef\u4fe1\u606f\u5e76\u4ee5\u72b6\u6001\u78011\u9000\u51fa\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><code>sys.exit()<\/code>\u4e0e\u5f02\u5e38\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u867d\u7136<code>sys.exit()<\/code>\u53ef\u4ee5\u7528\u4e8e\u9000\u51fa\u7a0b\u5e8f\uff0c\u4f46\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5f02\u5e38\u5904\u7406\u673a\u5236\u66f4\u4e3a\u7075\u6d3b\u548c\u5f3a\u5927\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u7b80\u5355\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528<code>sys.exit()<\/code>\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>sys.exit()<\/code>\u4f1a\u5f15\u53d1\u4e00\u4e2a<code>SystemExit<\/code>\u5f02\u5e38\uff0c\u5982\u679c\u4e0d\u60f3\u8ba9\u7a0b\u5e8f\u9000\u51fa\uff0c\u53ef\u4ee5\u5728<code>try-except<\/code>\u5757\u4e2d\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>try:<\/p>\n<p>    sys.exit(0)<\/p>\n<p>except SystemExit:<\/p>\n<p>    print(&quot;Caught SystemExit exception.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5728\u6355\u83b7\u5230<code>SystemExit<\/code>\u5f02\u5e38\u540e\u4e0d\u4f1a\u9000\u51fa\uff0c\u800c\u662f\u6267\u884c<code>except<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6761\u4ef6\u5224\u65ad\u903b\u8f91<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u903b\u8f91\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6765\u63a7\u5236\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u3002\u5f53\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u505c\u6b62\u6267\u884c\u6216\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>if<\/code>\u8bed\u53e5<\/li>\n<\/ol>\n<p><p><code>if<\/code>\u8bed\u53e5\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u3002\u901a\u8fc7<code>if<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u7684\u771f\u5047\u6765\u51b3\u5b9a\u662f\u5426\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;Enter &#39;stop&#39; to terminate the program: &quot;)<\/p>\n<p>if user_input == &#39;stop&#39;:<\/p>\n<p>    print(&quot;Terminating the program.&quot;)<\/p>\n<p>    exit(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u201cstop\u201d\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370\u4fe1\u606f\u5e76\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u590d\u6742\u6761\u4ef6\u5224\u65ad<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6761\u4ef6\u5224\u65ad\u901a\u5e38\u4f1a\u6d89\u53ca\u591a\u4e2a\u6761\u4ef6\u7684\u7ec4\u5408\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>and<\/code>\u3001<code>or<\/code>\u3001<code>not<\/code>\u7b49\u903b\u8f91\u8fd0\u7b97\u7b26\u6765\u7ec4\u5408\u591a\u4e2a\u6761\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">temperature = float(input(&quot;Enter the temperature: &quot;))<\/p>\n<p>humidity = float(input(&quot;Enter the humidity: &quot;))<\/p>\n<p>if temperature &gt; 30 and humidity &gt; 70:<\/p>\n<p>    print(&quot;High temperature and humidity detected. Exiting program.&quot;)<\/p>\n<p>    exit(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u6839\u636e\u6e29\u5ea6\u548c\u6e7f\u5ea6\u7684\u7ec4\u5408\u6761\u4ef6\u6765\u51b3\u5b9a\u662f\u5426\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528<code>assert<\/code>\u8bed\u53e5<\/li>\n<\/ol>\n<p><p><code>assert<\/code>\u8bed\u53e5\u7528\u4e8e\u5728\u7a0b\u5e8f\u4e2d\u68c0\u67e5\u67d0\u4e2a\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u3002\u5982\u679c\u6761\u4ef6\u4e3a\u5047\uff0c\u7a0b\u5e8f\u4f1a\u5f15\u53d1<code>AssertionError<\/code>\u5f02\u5e38\u3002<code>assert<\/code>\u8bed\u53e5\u901a\u5e38\u7528\u4e8e\u8c03\u8bd5\u548c\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u67d0\u4e9b\u91cd\u8981\u7684\u5047\u8bbe\u6210\u7acb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    assert b != 0, &quot;Division by zero!&quot;<\/p>\n<p>    return a \/ b<\/p>\n<p>result = divide(10, 2)<\/p>\n<p>print(&quot;Result:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>assert<\/code>\u8bed\u53e5\u68c0\u67e5<code>b<\/code>\u662f\u5426\u4e3a\u96f6\uff0c\u5982\u679c\u4e3a\u96f6\uff0c\u7a0b\u5e8f\u4f1a\u5f15\u53d1<code>AssertionError<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4fe1\u53f7\u5904\u7406\u673a\u5236<\/p>\n<\/p>\n<p><p>\u4fe1\u53f7\u5904\u7406\u673a\u5236\u7528\u4e8e\u5904\u7406\u64cd\u4f5c\u7cfb\u7edf\u4f20\u9012\u7ed9\u7a0b\u5e8f\u7684\u4fe1\u53f7\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>signal<\/code>\u6a21\u5757\u6765\u5904\u7406\u8fd9\u4e9b\u4fe1\u53f7\uff0c\u4ece\u800c\u63a7\u5236\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4ec0\u4e48\u662f\u4fe1\u53f7<\/li>\n<\/ol>\n<p><p>\u4fe1\u53f7\u662f\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u901a\u77e5\u8fdb\u7a0b\u67d0\u4e2a\u4e8b\u4ef6\u7684\u53d1\u751f\u3002\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u5728\u7ec8\u7aef\u4e2d\u6309\u4e0b<code>Ctrl+C<\/code>\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u53d1\u9001\u4e00\u4e2a<code>SIGINT<\/code>\u4fe1\u53f7\u7ed9\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528<code>signal<\/code>\u6a21\u5757<\/li>\n<\/ol>\n<p><p>Python\u7684<code>signal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u4fe1\u53f7\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528<code>signal.signal()<\/code>\u51fd\u6570\u6765\u4e3a\u7279\u5b9a\u4fe1\u53f7\u6ce8\u518c\u4e00\u4e2a\u5904\u7406\u5668\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<p>import sys<\/p>\n<p>def signal_handler(sig, frame):<\/p>\n<p>    print(&quot;Signal received, exiting program.&quot;)<\/p>\n<p>    sys.exit(0)<\/p>\n<p>signal.signal(signal.SIGINT, signal_handler)<\/p>\n<p>print(&quot;Press Ctrl+C to exit.&quot;)<\/p>\n<p>while True:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u7528\u6237\u6309\u4e0b<code>Ctrl+C<\/code>\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7<code>SIGINT<\/code>\u4fe1\u53f7\u5e76\u8c03\u7528<code>signal_handler<\/code>\u51fd\u6570\uff0c\u4ece\u800c\u4f18\u96c5\u5730\u9000\u51fa\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u81ea\u5b9a\u4e49\u4fe1\u53f7\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u5904\u7406\u7cfb\u7edf\u4fe1\u53f7\u5916\uff0c<code>signal<\/code>\u6a21\u5757\u8fd8\u5141\u8bb8\u5f00\u53d1\u8005\u5b9a\u4e49\u81ea\u5b9a\u4e49\u4fe1\u53f7\u5904\u7406\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4e3a<code>SIGTERM<\/code>\u4fe1\u53f7\u6ce8\u518c\u4e00\u4e2a\u5904\u7406\u5668\uff0c\u4ee5\u4fbf\u5728\u7a0b\u5e8f\u6536\u5230\u7ec8\u6b62\u4fe1\u53f7\u65f6\u6267\u884c\u7279\u5b9a\u7684\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def cleanup_and_exit(sig, frame):<\/p>\n<p>    print(&quot;Performing cleanup before exiting.&quot;)<\/p>\n<p>    # \u6267\u884c\u6e05\u7406\u64cd\u4f5c<\/p>\n<p>    sys.exit(0)<\/p>\n<p>signal.signal(signal.SIGTERM, cleanup_and_exit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5728\u63a5\u6536\u5230<code>SIGTERM<\/code>\u4fe1\u53f7\u65f6\uff0c\u4f1a\u6267\u884c\u6e05\u7406\u64cd\u4f5c\uff0c\u7136\u540e\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/p>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u7684\u673a\u5236\uff0c\u5b83\u901a\u5e38\u7528\u4e8e\u5904\u7406\u9700\u8981\u5728\u4f7f\u7528\u540e\u8fdb\u884c\u6e05\u7406\u7684\u8d44\u6e90\uff0c\u5982\u6587\u4ef6\u3001\u7f51\u7edc\u8fde\u63a5\u7b49\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6765\u81ea\u52a8\u7ba1\u7406\u8fd9\u4e9b\u8d44\u6e90\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u901a\u8fc7\u5b9e\u73b0<code>__enter__<\/code>\u548c<code>__exit__<\/code>\u65b9\u6cd5\u6765\u7ba1\u7406\u8d44\u6e90\u3002<code>with<\/code>\u8bed\u53e5\u4f1a\u5728\u4ee3\u7801\u5757\u5f00\u59cb\u65f6\u8c03\u7528<code>__enter__<\/code>\u65b9\u6cd5\uff0c\u5728\u4ee3\u7801\u5757\u7ed3\u675f\u65f6\u8c03\u7528<code>__exit__<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>with<\/code>\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u5373\u4f7f\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u53d1\u751f\u5f02\u5e38\uff0c\u6587\u4ef6\u4e5f\u4f1a\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/li>\n<\/ol>\n<p><p>\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0<code>__enter__<\/code>\u548c<code>__exit__<\/code>\u65b9\u6cd5\u6765\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class DatabaseConnection:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        self.connection = self.connect_to_database()<\/p>\n<p>        return self.connection<\/p>\n<p>    def __exit__(self, exc_type, exc_val, exc_tb):<\/p>\n<p>        self.connection.close()<\/p>\n<p>    def connect_to_database(self):<\/p>\n<p>        # \u6a21\u62df\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>        print(&quot;Connecting to the database.&quot;)<\/p>\n<p>        return &quot;DatabaseConnectionObject&quot;<\/p>\n<p>with DatabaseConnection() as connection:<\/p>\n<p>    print(&quot;Using the connection:&quot;, connection)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>DatabaseConnection<\/code>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u8d1f\u8d23\u8fde\u63a5\u548c\u5173\u95ed\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><code>contextlib<\/code>\u6a21\u5757<\/li>\n<\/ol>\n<p><p>Python\u7684<code>contextlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7528\u4e8e\u7b80\u5316\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684\u5de5\u5177\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>contextlib.contextmanager<\/code>\u88c5\u9970\u5668\u6765\u5b9a\u4e49\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from contextlib import contextmanager<\/p>\n<p>@contextmanager<\/p>\n<p>def simple_context_manager():<\/p>\n<p>    print(&quot;Entering the context.&quot;)<\/p>\n<p>    yield<\/p>\n<p>    print(&quot;Exiting the context.&quot;)<\/p>\n<p>with simple_context_manager():<\/p>\n<p>    print(&quot;Inside the context.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>simple_context_manager<\/code>\u51fd\u6570\u4f7f\u7528\u751f\u6210\u5668\u8bed\u6cd5\u6765\u5b9e\u73b0\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528<code>atexit<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>atexit<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u6ce8\u518c\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u6267\u884c\u7684\u6e05\u7406\u51fd\u6570\u7684\u529f\u80fd\u3002\u8fd9\u4e9b\u51fd\u6570\u5728\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u65f6\u81ea\u52a8\u6267\u884c\uff0c\u65e0\u8bba\u7a0b\u5e8f\u662f\u7531\u4e8e\u6b63\u5e38\u9000\u51fa\u8fd8\u662f\u672a\u6355\u83b7\u7684\u5f02\u5e38\u800c\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6ce8\u518c\u9000\u51fa\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>atexit.register()<\/code>\u51fd\u6570\u6765\u6ce8\u518c\u4e00\u4e2a\u9000\u51fa\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import atexit<\/p>\n<p>def goodbye():<\/p>\n<p>    print(&quot;Goodbye, world!&quot;)<\/p>\n<p>atexit.register(goodbye)<\/p>\n<p>print(&quot;This is a test program.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u65e0\u8bba\u7a0b\u5e8f\u5982\u4f55\u7ec8\u6b62\uff0c\u90fd\u4f1a\u5728\u9000\u51fa\u65f6\u6253\u5370\u201cGoodbye, world!\u201d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u591a\u4e2a\u9000\u51fa\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u6ce8\u518c\u591a\u4e2a\u9000\u51fa\u51fd\u6570\uff0c\u5b83\u4eec\u4f1a\u6309\u6ce8\u518c\u987a\u5e8f\u9006\u5e8f\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import atexit<\/p>\n<p>def first():<\/p>\n<p>    print(&quot;First function.&quot;)<\/p>\n<p>def second():<\/p>\n<p>    print(&quot;Second function.&quot;)<\/p>\n<p>atexit.register(first)<\/p>\n<p>atexit.register(second)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u4f1a\u5148\u8c03\u7528<code>second<\/code>\u51fd\u6570\uff0c\u7136\u540e\u8c03\u7528<code>first<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u548c<code>atexit<\/code><\/li>\n<\/ol>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u548c<code>atexit<\/code>\u6a21\u5757\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u786e\u4fdd\u5728\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u6267\u884c\u5fc5\u8981\u7684\u6e05\u7406\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4e2d\u6ce8\u518c\u4e00\u4e2a\u9000\u51fa\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import atexit<\/p>\n<p>from contextlib import contextmanager<\/p>\n<p>@contextmanager<\/p>\n<p>def resource_manager():<\/p>\n<p>    print(&quot;Acquiring resource.&quot;)<\/p>\n<p>    atexit.register(lambda: print(&quot;Releasing resource.&quot;))<\/p>\n<p>    yield<\/p>\n<p>    print(&quot;Resource released by context manager.&quot;)<\/p>\n<p>with resource_manager():<\/p>\n<p>    print(&quot;Using resource.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8d44\u6e90\u4f1a\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u88ab\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5904\u7406\u5e76\u53d1\u4efb\u52a1\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>threading<\/code>\u548c<code>multiprocessing<\/code>\u6a21\u5757\u6765\u7ba1\u7406\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<ol>\n<li>\u591a\u7ebf\u7a0b<\/li>\n<\/ol>\n<p><p><code>threading<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f<code>threading.Thread<\/code>\u7c7b\u6216\u4f7f\u7528<code>threading.Thread<\/code>\u5bf9\u8c61\u6765\u521b\u5efa\u65b0\u7ebf\u7a0b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def worker():<\/p>\n<p>    print(&quot;Worker thread starting.&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(&quot;Worker thread finishing.&quot;)<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p>print(&quot;M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n thread finishing.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u521b\u5efa\u5e76\u542f\u52a8\u4e00\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\uff0c\u7136\u540e\u7b49\u5f85\u5176\u5b8c\u6210\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u591a\u8fdb\u7a0b<\/li>\n<\/ol>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u7ba1\u7406\u8fdb\u7a0b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import time<\/p>\n<p>def worker():<\/p>\n<p>    print(&quot;Worker process starting.&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(&quot;Worker process finishing.&quot;)<\/p>\n<p>process = multiprocessing.Process(target=worker)<\/p>\n<p>process.start()<\/p>\n<p>process.join()<\/p>\n<p>print(&quot;Main process finishing.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e3b\u8fdb\u7a0b\u521b\u5efa\u5e76\u542f\u52a8\u4e00\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\uff0c\u7136\u540e\u7b49\u5f85\u5176\u5b8c\u6210\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u7684\u7ec8\u6b62<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u7ec8\u6b62\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u3002\u53ef\u4ee5\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u6267\u884c\uff0c\u6216\u4f7f\u7528<code>multiprocessing.Process.terminate()<\/code>\u65b9\u6cd5\u6765\u7ec8\u6b62\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>stop_event = threading.Event()<\/p>\n<p>def worker():<\/p>\n<p>    while not stop_event.is_set():<\/p>\n<p>        print(&quot;Worker thread running.&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>stop_event.set()<\/p>\n<p>thread.join()<\/p>\n<p>print(&quot;Main thread finishing.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u63a7\u5236\u5de5\u4f5c\u7ebf\u7a0b\u7684\u6267\u884c\uff0c\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u5728\u4efb\u610f\u65f6\u523b\u8bbe\u7f6e\u4e8b\u4ef6\u4ee5\u505c\u6b62\u5de5\u4f5c\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u7a0b\u5e8f\u65f6\uff0c\u65e5\u5fd7\u8bb0\u5f55\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\u3002Python\u7684<code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u4fe1\u606f\u3001\u9519\u8bef\u548c\u8c03\u8bd5\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>\u8981\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\uff0c\u9996\u5148\u9700\u8981\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668\uff0c\u7136\u540e\u4f7f\u7528<code>logger<\/code>\u5bf9\u8c61\u8bb0\u5f55\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.info(&quot;This is an info message.&quot;)<\/p>\n<p>logger.warning(&quot;This is a warning message.&quot;)<\/p>\n<p>logger.error(&quot;This is an error message.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>logger<\/code>\u5bf9\u8c61\u7528\u4e8e\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u914d\u7f6e\u65e5\u5fd7\u683c\u5f0f<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>basicConfig<\/code>\u51fd\u6570\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7684\u683c\u5f0f\u548c\u8f93\u51fa\u4f4d\u7f6e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">logging.basicConfig(<\/p>\n<p>    level=logging.INFO,<\/p>\n<p>    format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;,<\/p>\n<p>    handlers=[<\/p>\n<p>        logging.FileHandler(&#39;app.log&#39;),<\/p>\n<p>        logging.StreamHandler()<\/p>\n<p>    ]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u65e5\u5fd7\u4fe1\u606f\u88ab\u8bb0\u5f55\u5230\u6587\u4ef6<code>app.log<\/code>\u4e2d\uff0c\u540c\u65f6\u4e5f\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u8c03\u8bd5\u4fe1\u606f<\/li>\n<\/ol>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u65e5\u5fd7\u8bb0\u5f55\u53ef\u4ee5\u7528\u4e8e\u8c03\u8bd5\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bb0\u5f55\u51fd\u6570\u7684\u8f93\u5165\u53c2\u6570\u3001\u8f93\u51fa\u7ed3\u679c\u548c\u5f02\u5e38\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    logger.info(&quot;Dividing %d by %d&quot;, a, b)<\/p>\n<p>    try:<\/p>\n<p>        result = a \/ b<\/p>\n<p>    except ZeroDivisionError as e:<\/p>\n<p>        logger.error(&quot;Error dividing by zero: %s&quot;, e)<\/p>\n<p>        raise<\/p>\n<p>    logger.info(&quot;Result: %f&quot;, result)<\/p>\n<p>    return result<\/p>\n<p>try:<\/p>\n<p>    divide(10, 0)<\/p>\n<p>except Exception as e:<\/p>\n<p>    logger.exception(&quot;Exception occurred: %s&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u65e5\u5fd7\u8bb0\u5f55\u4e86\u9664\u6cd5\u64cd\u4f5c\u7684\u8f93\u5165\u53c2\u6570\u3001\u7ed3\u679c\u548c\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u4f7f\u7528\u65ad\u8a00\u8fdb\u884c\u8c03\u8bd5<\/p>\n<\/p>\n<p><p>\u65ad\u8a00\u662f\u4e00\u79cd\u7528\u4e8e\u68c0\u67e5\u7a0b\u5e8f\u4e2d\u5173\u952e\u5047\u8bbe\u7684\u8c03\u8bd5\u5de5\u5177\u3002\u901a\u8fc7\u65ad\u8a00\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u63d2\u5165\u68c0\u67e5\u4ee3\u7801\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u67d0\u4e9b\u6761\u4ef6\u6210\u7acb\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>\u65ad\u8a00\u4f7f\u7528<code>assert<\/code>\u8bed\u53e5\u5b9e\u73b0\uff0c\u8bed\u6cd5\u4e3a<code>assert condition, message<\/code>\u3002\u5982\u679c<code>condition<\/code>\u4e3a\u5047\uff0c\u7a0b\u5e8f\u4f1a\u5f15\u53d1<code>AssertionError<\/code>\u5f02\u5e38\uff0c\u5e76\u663e\u793a<code>message<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"><\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7a0b\u5e8f\u5931\u6548\u7684\u673a\u5236\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7a0b\u5e8f\u5931\u6548\u7684\u673a\u5236\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u8bbe\u7f6e\u7279\u5b9a\u7684\u6761\u4ef6\u5224\u65ad\uff0c\u5f53\u6ee1\u8db3\u6761\u4ef6\u65f6\uff0c\u89e6\u53d1\u5f02\u5e38\u6216\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\uff0c\u7ec8\u6b62\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b9a\u65f6\u5668\u6216\u7279\u5b9a\u7684\u8f93\u5165\u6765\u68c0\u6d4b\u7a0b\u5e8f\u7684\u6709\u6548\u6027\uff0c\u82e5\u8f93\u5165\u4e0d\u7b26\u5408\u9884\u8bbe\u6807\u51c6\uff0c\u5219\u8ba9\u7a0b\u5e8f\u5931\u6548\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u6765\u63a7\u5236Python\u7a0b\u5e8f\u7684\u5931\u6548\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u73af\u5883\u53d8\u91cf\u80fd\u591f\u5f71\u54cdPython\u7a0b\u5e8f\u7684\u6267\u884c\u3002\u5982\u679c\u4f60\u5728\u7a0b\u5e8f\u4e2d\u68c0\u67e5\u7279\u5b9a\u7684\u73af\u5883\u53d8\u91cf\uff0c\u5e76\u6839\u636e\u5176\u503c\u51b3\u5b9a\u7a0b\u5e8f\u7684\u884c\u4e3a\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u7a0b\u5e8f\u7684\u5931\u6548\u3002\u4f8b\u5982\uff0c\u5f53\u67d0\u4e2a\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u201cFalse\u201d\u65f6\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u4e3b\u52a8\u9000\u51fa\u6216\u629b\u51fa\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u7a0b\u5e8f\u5931\u6548\u540e\u7684\u72b6\u6001\u6062\u590d\uff1f<\/strong><br \/>\u5904\u7406\u7a0b\u5e8f\u5931\u6548\u540e\u7684\u72b6\u6001\u6062\u590d\u901a\u5e38\u6d89\u53ca\u5f02\u5e38\u5904\u7406\u548c\u6301\u4e45\u5316\u6570\u636e\u3002\u901a\u8fc7try-except\u5757\u6355\u83b7\u5f02\u5e38\u540e\uff0c\u53ef\u4ee5\u8bb0\u5f55\u5f53\u524d\u72b6\u6001\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u4ee5\u4fbf\u5728\u4e0b\u6b21\u8fd0\u884c\u65f6\u6062\u590d\u3002\u786e\u4fdd\u7a0b\u5e8f\u5728\u4e0b\u6b21\u542f\u52a8\u65f6\u80fd\u6839\u636e\u4fdd\u5b58\u7684\u72b6\u6001\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u6267\u884c\u6216\u91cd\u65b0\u521d\u59cb\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8ba9\u7a0b\u5e8f\u5931\u6548\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u673a\u5236\u6355\u83b7\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u3001\u4f7f\u7528sys.exit()\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1018644,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018640"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1018640"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018640\/revisions"}],"predecessor-version":[{"id":1018645,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018640\/revisions\/1018645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1018644"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1018640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1018640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1018640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}