{"id":2866,"date":"2025-06-01T17:03:58","date_gmt":"2025-06-04T09:32:57","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2866"},"modified":"2025-06-04T17:03:58","modified_gmt":"2025-06-04T09:32:57","slug":"%e4%bd%bf%e7%94%a8python%e7%9a%84withopen%e5%87%bd%e6%95%b8%e8%87%aa%e5%8b%95%e9%97%9c%e9%96%89%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8python%e7%9a%84withopen%e5%87%bd%e6%95%b8%e8%87%aa%e5%8b%95%e9%97%9c%e9%96%89%e6%96%87%e4%bb%b6\/","title":{"rendered":"\u4f7f\u7528 Python \u7684 `with open()` \u51fd\u6578\u81ea\u52d5\u95dc\u9589\u6587\u4ef6\u7684\u6700\u4f73\u5be6\u8e10\uff082025 \u66f4\u65b0\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, with open(), \u6587\u4ef6\u64cd\u4f5c\"><\/p>\n<p>Python \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u4e14\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u958b\u767c\u9818\u57df\u3002\u5728 Python \u4e2d\uff0c`with open()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u8a9e\u6cd5\u7279\u6027\uff0c\u5b83\u80fd\u5920\u8b93\u958b\u767c\u8005\u8f15\u9b06\u5730\u6253\u958b\u6587\u4ef6\uff0c\u4e26\u5728\u64cd\u4f5c\u5b8c\u6210\u5f8c\u81ea\u52d5\u95dc\u9589\u6587\u4ef6\uff0c\u907f\u514d\u8cc7\u6e90\u6cc4\u6f0f\u7b49\u554f\u984c\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f `with open()` \u51fd\u6578\uff1f<\/h2>\n<p>`with open()` \u662f Python \u4e2d\u7684\u4e00\u500b\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u5b83\u7684\u4e3b\u8981\u529f\u80fd\u662f\u81ea\u52d5\u7ba1\u7406\u6587\u4ef6\u7684\u958b\u555f\u8207\u95dc\u9589\u3002\u4f7f\u7528 `with open()` \u8a9e\u6cd5\u53ef\u4ee5\u78ba\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u5f8c\u81ea\u52d5\u95dc\u9589\uff0c\u9019\u6a23\u958b\u767c\u8005\u4e0d\u9700\u8981\u624b\u52d5\u8abf\u7528 `close()` \u51fd\u6578\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 `with open()` \u51fd\u6578\uff1f<\/h2>\n<p>\u4f7f\u7528 `with open()` \u51fd\u6578\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u5728\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4e2d\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\uff0c\u5373\u53ef\u8f15\u9b06\u5730\u6253\u958b\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u4f7f\u7528\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nwith open(&#8216;test.txt&#8217;, &#8216;r&#8217;) as f:<br \/>\n    data = f.read()<br \/>\n    print(data)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 `with open()` \u6253\u958b\u4e86\u4e00\u500b\u540d\u70ba `test.txt` \u7684\u6587\u4ef6\uff0c\u4f7f\u7528 `&#8217;r&#8217;` \u6a21\u5f0f\u8868\u793a\u4ee5\u53ea\u8b80\u65b9\u5f0f\u6253\u958b\u6587\u4ef6\u3002`f.read()` \u7528\u65bc\u8b80\u53d6\u6587\u4ef6\u5167\u5bb9\uff0c\u7136\u5f8c\u5c07\u5176\u6253\u5370\u51fa\u4f86\u3002<\/p>\n<h2>\u5e38\u898b\u7684\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f<\/h2>\n<p>\u5728\u4f7f\u7528 `with open()` \u6642\uff0c\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u4e0d\u540c\u7684\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\uff1a<br \/>\n&#8211; `&#8217;r&#8217;`\uff1a\u53ea\u8b80\u6a21\u5f0f\uff08\u9810\u8a2d\uff09<br \/>\n&#8211; `&#8217;w&#8217;`\uff1a\u5beb\u5165\u6a21\u5f0f\uff0c\u6703\u8986\u84cb\u539f\u6709\u5167\u5bb9<br \/>\n&#8211; `&#8217;a&#8217;`\uff1a\u9644\u52a0\u6a21\u5f0f\uff0c\u6703\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5167\u5bb9<br \/>\n&#8211; `&#8217;b&#8217;`\uff1a\u4e8c\u9032\u5236\u6a21\u5f0f\uff0c\u7528\u65bc\u8655\u7406\u975e\u6587\u672c\u6587\u4ef6\uff08\u4f8b\u5982\u5716\u7247\uff09<\/p>\n<h2>`with open()` \u51fd\u6578\u7684\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 `with open()` \u51fd\u6578\u7684\u4e3b\u8981\u512a\u9ede\u5305\u62ec\uff1a<br \/>\n1. **\u81ea\u52d5\u95dc\u9589\u6587\u4ef6**\uff1a\u5373\u4f7f\u5728\u8b80\u53d6\u6216\u5beb\u5165\u904e\u7a0b\u4e2d\u767c\u751f\u932f\u8aa4\uff0c\u6587\u4ef6\u4e5f\u6703\u88ab\u6b63\u78ba\u95dc\u9589\uff0c\u907f\u514d\u8cc7\u6e90\u6cc4\u6f0f\u3002<br \/>\n2. **\u63d0\u9ad8\u4ee3\u78bc\u53ef\u8b80\u6027**\uff1a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4f7f\u5f97\u4ee3\u78bc\u7d50\u69cb\u66f4\u6e05\u6670\uff0c\u6613\u65bc\u7406\u89e3\u3002<br \/>\n3. **\u964d\u4f4e\u51fa\u932f\u6a5f\u7387**\uff1a\u81ea\u52d5\u7ba1\u7406\u6587\u4ef6\u7684\u958b\u555f\u8207\u95dc\u9589\uff0c\u964d\u4f4e\u4e86\u56e0\u5fd8\u8a18\u95dc\u9589\u6587\u4ef6\u800c\u5f15\u8d77\u7684\u932f\u8aa4\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `with open()` \u7684\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; **FileNotFoundError**\uff1a\u7576\u6307\u5b9a\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\u6642\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u78ba\u4fdd\u6587\u4ef6\u8def\u5f91\u6b63\u78ba\u3002<br \/>\n&#8211; **PermissionError**\uff1a\u7576\u6c92\u6709\u6b0a\u9650\u8a2a\u554f\u6587\u4ef6\u6642\u6703\u5f15\u767c\u6b64\u932f\u8aa4\uff0c\u6aa2\u67e5\u6587\u4ef6\u548c\u8cc7\u6599\u593e\u7684\u6b0a\u9650\u8a2d\u7f6e\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`with open()` \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u6587\u672c\u6587\u4ef6\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u8b80\u53d6\u548c\u5beb\u5165\u4e8c\u9032\u5236\u6587\u4ef6\uff0c\u4f8b\u5982\u5716\u7247\u6216\u97f3\u983b\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u5beb\u5165\u4e8c\u9032\u5236\u6587\u4ef6\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nwith open(&#8216;image.png&#8217;, &#8216;rb&#8217;) as f:<br \/>\n    img_data = f.read()<br \/>\n    # \u8655\u7406 img_data<br \/>\n&#8220;`<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u7684 `with open()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u4e0d\u50c5\u63d0\u9ad8\u4e86\u958b\u767c\u6548\u7387\uff0c\u9084\u964d\u4f4e\u4e86\u51fa\u932f\u7684\u6a5f\u7387\u3002\u7121\u8ad6\u662f\u8655\u7406\u6587\u672c\u6587\u4ef6\u9084\u662f\u4e8c\u9032\u5236\u6587\u4ef6\uff0c`with open()` \u90fd\u662f\u6700\u4f73\u9078\u64c7\u3002<\/p>\n<p>\u5982\u9700\u6df1\u5165\u4e86\u89e3 Python \u6587\u4ef6\u64cd\u4f5c\u53ca\u76f8\u95dc\u6280\u5de7\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/xyz) \u4f86\u63d0\u5347\u60a8\u7684\u7de8\u7a0b\u80fd\u529b\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1\uff1a`with open()` \u51fd\u6578\u662f\u5426\u9069\u7528\u65bc\u6240\u6709 Python \u7248\u672c\uff1f**<br \/>\nA1\uff1a`with open()` \u51fd\u6578\u5728 Python 2.5 \u53ca\u4ee5\u5f8c\u7684\u7248\u672c\u90fd\u53ef\u7528\uff0c\u56e0\u6b64\u5728\u4f7f\u7528 Python 3.x \u6642\uff0c\u60a8\u53ef\u4ee5\u5b89\u5168\u5730\u4f7f\u7528\u5b83\u3002<\/p>\n<p>**Q2\uff1a\u5982\u4f55\u8655\u7406\u5927\u6587\u4ef6\u7684\u8b80\u53d6\uff1f**<br \/>\nA2\uff1a\u53ef\u4ee5\u4f7f\u7528 `for` \u5faa\u74b0\u9010\u884c\u8b80\u53d6\u6587\u4ef6\uff0c\u907f\u514d\u4e00\u6b21\u6027\u8b80\u53d6\u6574\u500b\u6587\u4ef6\u5c0e\u81f4\u5167\u5b58\u4e0d\u8db3\u3002<\/p>\n<p>**Q3\uff1a\u5982\u679c\u6211\u9700\u8981\u5728\u6587\u4ef6\u64cd\u4f5c\u5f8c\u57f7\u884c\u6e05\u7406\u64cd\u4f5c\uff0c\u8a72\u600e\u9ebc\u8fa6\uff1f**<br \/>\nA3\uff1a\u53ef\u4ee5\u4f7f\u7528 `try&#8230;finally` \u7d50\u69cb\u6216\u5c07\u6e05\u7406\u4ee3\u78bc\u653e\u5728 `with` \u8a9e\u53e5\u5916\u90e8\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684withopen()\u51fd\u6578\u53ef\u4ee5\u8b93\u958b\u555f\u7684\u6587\u4ef6\u81ea\u52d5\u95dc\u9589\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u7ba1\u7406\u6587\u4ef6\uff0c\u4e26\u78ba\u4fdd\u6587\u4ef6\u7684\u5b89\u5168\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39withopen()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u81ea\u52d5\u95dc\u9589\u6587\u4ef6\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2866","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Ke","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2866"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2866\/revisions"}],"predecessor-version":[{"id":2867,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2866\/revisions\/2867"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}