{"id":951304,"date":"2024-12-27T00:56:21","date_gmt":"2024-12-26T16:56:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/951304.html"},"modified":"2024-12-27T00:56:24","modified_gmt":"2024-12-26T16:56:24","slug":"python-%e5%a6%82%e4%bd%95%e5%85%b3%e9%97%ad%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/951304.html","title":{"rendered":"python \u5982\u4f55\u5173\u95ed\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085549\/e02a9948-f406-42a6-980e-3768a743ab95.webp\" alt=\"python \u5982\u4f55\u5173\u95ed\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5173\u95ed\u6587\u4ef6\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>close()<\/code>\u65b9\u6cd5\u3001\u901a\u8fc7<code>with<\/code>\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u5173\u95ed\u3001\u786e\u4fdd\u8d44\u6e90\u4e0d\u88ab\u6d6a\u8d39\u4e14\u6570\u636e\u5b8c\u6574\u3002<\/strong>\u4f7f\u7528<code>close()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u624b\u52a8\u63a7\u5236\u6587\u4ef6\u5173\u95ed\uff0c\u786e\u4fdd\u6587\u4ef6\u7684\u6240\u6709\u64cd\u4f5c\u5b8c\u6210\u540e\u8d44\u6e90\u88ab\u91ca\u653e\uff1b<code>with<\/code>\u8bed\u53e5\u662f\u4e00\u79cd\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u80fd\u591f\u5728\u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6bd5\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u907f\u514d\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\u9020\u6210\u7684\u8d44\u6e90\u6cc4\u6f0f\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e24\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>close()<\/code>\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6587\u4ef6\u64cd\u4f5c\u662f\u975e\u5e38\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u5fc5\u987b\u786e\u4fdd\u53ca\u65f6\u5173\u95ed\uff0c\u4ee5\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90\u3002\u4f7f\u7528<code>close()<\/code>\u65b9\u6cd5\u624b\u52a8\u5173\u95ed\u6587\u4ef6\u662f\u4e00\u79cd\u57fa\u672c\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u6211\u4eec\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u65f6\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\u3002\u901a\u8fc7\u8be5\u5bf9\u8c61\u7684<code>close()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5173\u95ed\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<h2><strong>\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example.txt<\/code>\u7684\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u64cd\u4f5c\uff0c\u4e4b\u540e\u8c03\u7528<code>close()<\/code>\u65b9\u6cd5\u6765\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u91cd\u8981\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u624b\u52a8\u5173\u95ed\u6587\u4ef6\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u6253\u5f00\u7684\u6587\u4ef6\u6570\u91cf\u6709\u9650\u5236\u3002\u672a\u5173\u95ed\u7684\u6587\u4ef6\u4f1a\u5bfc\u81f4\u8d44\u6e90\u6d6a\u8d39\uff0c\u751a\u81f3\u53ef\u80fd\u5f15\u53d1\u7a0b\u5e8f\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u6587\u4ef6\u672a\u5173\u95ed\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u672a\u80fd\u6b63\u786e\u5199\u5165\u78c1\u76d8\uff0c\u6216\u8005\u7a0b\u5e8f\u5360\u7528\u8fc7\u591a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bfc\u81f4\u7cfb\u7edf\u9519\u8bef\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5bfc\u81f4<code>close()<\/code>\u65b9\u6cd5\u65e0\u6cd5\u6267\u884c\u3002\u4e3a\u6b64\uff0c\u901a\u5e38\u4f7f\u7528<code>try...finally<\/code>\u8bed\u53e5\u6765\u786e\u4fdd\u6587\u4ef6\u5173\u95ed\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>try:<\/p>\n<p>    # \u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c<\/p>\n<p>    pass<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u65e0\u8bba\u6587\u4ef6\u64cd\u4f5c\u662f\u5426\u6210\u529f\uff0c\u6700\u7ec8\u90fd\u4f1a\u6267\u884c<code>finally<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\uff0c\u786e\u4fdd\u6587\u4ef6\u88ab\u5173\u95ed\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u81ea\u52a8\u5173\u95ed\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>with<\/code>\u8bed\u53e5\u662fPython\u4e2d\u5904\u7406\u6587\u4ef6\u7684\u63a8\u8350\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u6781\u5927\u5730\u7b80\u5316\u4e86\u6587\u4ef6\u64cd\u4f5c\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u65f6\uff0c\u6587\u4ef6\u5728\u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6bd5\u540e\u4f1a\u81ea\u52a8\u5173\u95ed\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>    # \u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c<\/p>\n<p>    content = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>with<\/code>\u8bed\u53e5\u4f1a\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u5173\u95ed\uff0c\u65e0\u9700\u624b\u52a8\u8c03\u7528<code>close()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u52bf<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\u6027<\/strong>\uff1a<code>with<\/code>\u8bed\u53e5\u7b80\u5316\u4e86\u4ee3\u7801\u7ed3\u6784\uff0c\u51cf\u5c11\u4e86\u5bf9\u5f02\u5e38\u5904\u7406\u7684\u9700\u6c42\uff0c\u907f\u514d\u4e86\u9057\u6f0f\u5173\u95ed\u6587\u4ef6\u7684\u98ce\u9669\u3002<\/li>\n<li><strong>\u5b89\u5168\u6027<\/strong>\uff1a\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\uff0c\u51cf\u5c11\u8d44\u6e90\u6cc4\u6f0f\u7684\u53ef\u80fd\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<p><p><code>with<\/code>\u8bed\u53e5\u9002\u7528\u4e8e\u5927\u591a\u6570\u6587\u4ef6\u64cd\u4f5c\u573a\u666f\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u8fdb\u884c\u591a\u6b21\u6587\u4ef6\u64cd\u4f5c\u65f6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;input.txt&#39;, &#39;r&#39;) as infile, open(&#39;output.txt&#39;, &#39;w&#39;) as outfile:<\/p>\n<p>    for line in infile:<\/p>\n<p>        outfile.write(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u6f14\u793a\u4e86\u540c\u65f6\u6253\u5f00\u4e24\u4e2a\u6587\u4ef6\uff0c\u5e76\u5728<code>with<\/code>\u8bed\u53e5\u5757\u5185\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u63a8\u8350\u4f7f\u7528<code>with<\/code>\u8bed\u53e5<\/strong>\uff1a\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5efa\u8bae\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\uff0c\u56e0\u4e3a\u5176\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u7684\u7279\u6027\u53ef\u4ee5\u51cf\u5c11\u9519\u8bef\u7684\u53d1\u751f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e86\u89e3<code>close()<\/code>\u65b9\u6cd5\u7684\u7528\u6cd5<\/strong>\uff1a\u5c3d\u7ba1<code>with<\/code>\u8bed\u53e5\u66f4\u4e3a\u7b80\u6d01\uff0c\u4f46\u4e86\u89e3\u5982\u4f55\u624b\u52a8\u5173\u95ed\u6587\u4ef6\u4ee5\u53ca\u5728\u4f55\u65f6\u4f7f\u7528<code>try...finally<\/code>\u7ed3\u6784\u6765\u786e\u4fdd\u6587\u4ef6\u5173\u95ed\u4ecd\u7136\u662f\u5f88\u6709\u5fc5\u8981\u7684\u6280\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u6587\u4ef6\u64cd\u4f5c\u5f02\u5e38<\/strong>\uff1a\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u8fd0\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406Python\u4e2d\u7684\u6587\u4ef6\u8d44\u6e90\uff0c\u786e\u4fdd\u7a0b\u5e8f\u8fd0\u884c\u7684\u7a33\u5b9a\u6027\u548c\u9ad8\u6548\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u786e\u4fdd\u6587\u4ef6\u5728\u5904\u7406\u540e\u88ab\u6b63\u786e\u5173\u95ed\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u5904\u7406\u540e\u88ab\u6b63\u786e\u5173\u95ed\u3002<code>with<\/code>\u8bed\u53e5\u4f1a\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u65e0\u9700\u624b\u52a8\u8c03\u7528<code>close()<\/code>\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:\n    data = file.read()\n# \u6587\u4ef6\u5728\u6b64\u5904\u5df2\u81ea\u52a8\u5173\u95ed\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528<code>close()<\/code>\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>close()<\/code>\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u5728\u5bf9\u6587\u4ef6\u7684\u6240\u6709\u64cd\u4f5c\u5b8c\u6210\u540e\u8c03\u7528\u8be5\u65b9\u6cd5\u3002\u540c\u65f6\uff0c\u6700\u597d\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5df2\u7ecf\u6253\u5f00\uff0c\u4ee5\u907f\u514d\u53ef\u80fd\u7684\u9519\u8bef\u3002\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u6765\u6355\u83b7\u6587\u4ef6\u672a\u6253\u5f00\u65f6\u7684\u60c5\u51b5\u3002\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    file = open(&#39;example.txt&#39;, &#39;r&#39;)\n    # \u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\nfinally:\n    file.close()  # \u786e\u4fdd\u6587\u4ef6\u88ab\u5173\u95ed\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u5728\u5904\u7406\u6587\u4ef6\u65f6\u53d1\u751f\u5f02\u5e38\uff0c\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u5417\uff1f<\/strong><br \/>\u5728\u4e0d\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u5728\u6587\u4ef6\u64cd\u4f5c\u65f6\u53d1\u751f\u5f02\u5e38\uff0c\u6587\u4ef6\u5c06\u4e0d\u4f1a\u81ea\u52a8\u5173\u95ed\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u6cc4\u9732\u3002\u4f7f\u7528<code>try...finally<\/code>\u7ed3\u6784\u53ef\u4ee5\u786e\u4fdd\u5373\u4f7f\u53d1\u751f\u5f02\u5e38\uff0c\u6587\u4ef6\u4e5f\u4f1a\u88ab\u5173\u95ed\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)\ntry:\n    # \u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\n    data = file.read()\nexcept Exception as e:\n    print(f&quot;\u53d1\u751f\u9519\u8bef: {e}&quot;)\nfinally:\n    file.close()  # \u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u6267\u884c\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5173\u95ed\u6587\u4ef6\u7684\u65b9\u6cd5\u662f\u4f7f\u7528close()\u65b9\u6cd5\u3001\u901a\u8fc7with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u5173\u95ed\u3001\u786e\u4fdd\u8d44\u6e90\u4e0d\u88ab\u6d6a\u8d39\u4e14\u6570 [&hellip;]","protected":false},"author":3,"featured_media":951306,"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\/951304"}],"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=951304"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/951304\/revisions"}],"predecessor-version":[{"id":951308,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/951304\/revisions\/951308"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/951306"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=951304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=951304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=951304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}