{"id":3584,"date":"2025-05-28T15:47:03","date_gmt":"2025-06-04T07:53:02","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3584"},"modified":"2025-06-04T15:47:03","modified_gmt":"2025-06-04T07:53:02","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84filenotfounderror%e4%be%8b%e5%a4%96%e8%99%95%e7%90%86","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84filenotfounderror%e4%be%8b%e5%a4%96%e8%99%95%e7%90%86\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 FileNotFoundError \u4f8b\u5916\u8655\u7406\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, FileNotFoundError, \u4f8b\u5916\u8655\u7406, \u932f\u8aa4\u6392\u9664, Python \u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 FileNotFoundError \u4f8b\u5916\u8655\u7406<\/h1>\n<p>\u5728 Python \u4e2d\uff0cFileNotFoundError \u662f\u4e00\u500b\u5e38\u898b\u7684\u4f8b\u5916\uff0c\u7576\u7a0b\u5f0f\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u6a94\u6848\u6642\uff0c\u4fbf\u6703\u62cb\u51fa\u6b64\u4f8b\u5916\u3002\u9019\u500b\u4f8b\u5916\u662f OSError \u7684\u5b50\u985e\u5225\uff0c\u56e0\u6b64\u4e5f\u6703\u8207\u5176\u4ed6 OS \u76f8\u95dc\u7684\u4f8b\u5916\uff08\u5982 PermissionError\uff09\u4e00\u8d77\u8655\u7406\u3002<\/p>\n<h2>FileNotFoundError \u7684\u767c\u751f\u60c5\u6cc1<\/h2>\n<p>FileNotFoundError \u53ef\u80fd\u6703\u5728\u4ee5\u4e0b\u60c5\u6cc1\u4e2d\u767c\u751f\uff1a<\/p>\n<ul>\n<li>\u7576\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u6a94\u6848\u6642<\/li>\n<li>\u7576\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u6a94\u6848\uff0c\u4f46\u6c92\u6709\u8db3\u5920\u7684\u6b0a\u9650\u6642<\/li>\n<li>\u7576\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u6a94\u6848\uff0c\u4f46\u6a94\u6848\u5df2\u7d93\u88ab\u522a\u9664\u6642<\/li>\n<\/ul>\n<h2>\u5982\u4f55\u6709\u6548\u8655\u7406 FileNotFoundError<\/h2>\n<p>\u7576 FileNotFoundError \u767c\u751f\u6642\uff0c\u6700\u597d\u7684\u8655\u7406\u65b9\u5f0f\u662f\u4f7f\u7528 <code>try\/except<\/code> \u8a9e\u53e5\u4f86\u6355\u7372\u9019\u500b\u4f8b\u5916\uff0c\u4e26\u57f7\u884c\u76f8\u61c9\u7684\u8655\u7406\u7a0b\u5e8f\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntry:<br \/>\n    f = open(&#8216;myfile.txt&#8217;)<br \/>\nexcept FileNotFoundError:<br \/>\n    print(&#8216;\u6a94\u6848\u4e0d\u5b58\u5728\uff0c\u8acb\u6aa2\u67e5\u6a94\u6848\u540d\u7a31\u6216\u8def\u5f91\u3002&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5617\u8a66\u6253\u958b\u4e00\u500b\u540d\u70ba <code>myfile.txt<\/code> \u7684\u6a94\u6848\u3002\u5982\u679c\u6a94\u6848\u4e0d\u5b58\u5728\uff0c\u5247\u6703\u62cb\u51fa FileNotFoundError \u4f8b\u5916\uff0c\u4e26\u57f7\u884c <code>print()<\/code> \u8a9e\u53e5\uff0c\u986f\u793a\u6a94\u6848\u4e0d\u5b58\u5728\u7684\u8a0a\u606f\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664\u6280\u5de7<\/p>\n<p>1. **\u78ba\u8a8d\u6a94\u6848\u8def\u5f91**\uff1a\u78ba\u4fdd\u4f60\u63d0\u4f9b\u7684\u6a94\u6848\u8def\u5f91\u662f\u6b63\u78ba\u7684\uff0c\u5305\u62ec\u6a94\u6848\u540d\u7a31\u548c\u64f4\u5c55\u540d\u3002<br \/>\n2. **\u6aa2\u67e5\u6a94\u6848\u6b0a\u9650**\uff1a\u5982\u679c\u6a94\u6848\u5b58\u5728\u4f46\u4ecd\u7136\u7121\u6cd5\u8a2a\u554f\uff0c\u6aa2\u67e5\u4f60\u7684\u61c9\u7528\u7a0b\u5e8f\u662f\u5426\u5177\u6709\u8db3\u5920\u7684\u6b0a\u9650\u3002<br \/>\n3. **\u4f7f\u7528 pathlib \u6a21\u7d44**\uff1a\u53ef\u4ee5\u4f7f\u7528 Python \u7684 pathlib \u6a21\u7d44\u4f86\u66f4\u65b9\u4fbf\u5730\u8655\u7406\u8def\u5f91\uff0c\u4e26\u6aa2\u67e5\u6a94\u6848\u662f\u5426\u5b58\u5728\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 pathlib \u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom pathlib import Path<\/p>\n<p>file_path = Path(&#8216;myfile.txt&#8217;)<\/p>\n<p>if file_path.is_file():<br \/>\n    with file_path.open() as f:<br \/>\n        content = f.read()<br \/>\nelse:<br \/>\n    print(&#8216;\u6a94\u6848\u4e0d\u5b58\u5728\uff0c\u8acb\u6aa2\u67e5\u6a94\u6848\u540d\u7a31\u6216\u8def\u5f91\u3002&#8217;)<br \/>\n&#8220;`<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528\uff1a\u81ea\u52d5\u5316\u6a94\u6848\u8655\u7406<\/h2>\n<p>\u82e5\u4f60\u9700\u8981\u81ea\u52d5\u8655\u7406\u591a\u500b\u6a94\u6848\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u8ff4\u5708\u7d50\u5408 FileNotFoundError \u7684\u8655\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfile_names = [&#8216;file1.txt&#8217;, &#8216;file2.txt&#8217;, &#8216;file3.txt&#8217;]\n<p>for name in file_names:<br \/>\n    try:<br \/>\n        with open(name) as f:<br \/>\n            print(f.read())<br \/>\n    except FileNotFoundError:<br \/>\n        print(f'{name} \u6a94\u6848\u4e0d\u5b58\u5728\uff0c\u8acb\u6aa2\u67e5\u6a94\u6848\u540d\u7a31\u6216\u8def\u5f91\u3002&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u5730\u8655\u7406\u591a\u500b\u6a94\u6848\u7684\u8b80\u53d6\uff0c\u4e26\u5728\u6a94\u6848\u4e0d\u5b58\u5728\u6642\u63d0\u4f9b\u5373\u6642\u7684\u53cd\u994b\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>FileNotFoundError \u662f\u4e00\u500b\u5e38\u898b\u7684 Python \u4f8b\u5916\uff0c\u901a\u5e38\u5728\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u6a94\u6848\u6642\u767c\u751f\u3002\u4f7f\u7528 <code>try\/except<\/code> \u8a9e\u53e5\u80fd\u6709\u6548\u6355\u7372\u6b64\u4f8b\u5916\uff0c\u4e26\u6839\u64da\u5be6\u969b\u9700\u8981\u9032\u884c\u76f8\u61c9\u7684\u932f\u8aa4\u8655\u7406\uff0c\u5f9e\u800c\u63d0\u5347\u7a0b\u5f0f\u7684\u5065\u58ef\u6027\u3002<\/p>\n<p>\u5982\u6b32\u4e86\u89e3\u66f4\u591a Python \u4f8b\u5916\u8655\u7406\u7684\u6280\u5de7\uff0c\u8acb\u53c3\u8003\u9019\u7bc7[Python \u4f8b\u5916\u8655\u7406\u6559\u5b78](https:\/\/vocus.cc\/article\/123456)\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/h2>\n<p>**Q1: \u5982\u4f55\u77e5\u9053\u6a94\u6848\u662f\u5426\u5b58\u5728\u800c\u4e0d\u5f15\u767c\u4f8b\u5916\uff1f**<br \/>\nA1: \u53ef\u4ee5\u4f7f\u7528 pathlib \u6a21\u7d44\u7684 <code>is_file()<\/code> \u65b9\u6cd5\u4f86\u6aa2\u67e5\u6a94\u6848\u662f\u5426\u5b58\u5728\uff0c\u9019\u6a23\u5c31\u4e0d\u9700\u8981\u5f15\u767c\u4f8b\u5916\u4e86\u3002<\/p>\n<p>**Q2: FileNotFoundError \u548c PermissionError \u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA2: FileNotFoundError \u7576\u4f60\u8a66\u5716\u8a2a\u554f\u4e0d\u5b58\u5728\u7684\u6a94\u6848\u6642\u62cb\u51fa\uff0c\u800c PermissionError \u5247\u662f\u5728\u4f60\u6c92\u6709\u6b0a\u9650\u8a2a\u554f\u6a94\u6848\u6642\u62cb\u51fa\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u5728\u8655\u7406\u4f8b\u5916\u6642\u8a18\u9304\u932f\u8aa4\u4fe1\u606f\uff1f**<br \/>\nA3: \u4f60\u53ef\u4ee5\u5728 <code>except<\/code> \u5340\u584a\u4e2d\u4f7f\u7528 logging \u6a21\u7d44\u4f86\u8a18\u9304\u932f\u8aa4\u4fe1\u606f\uff0c\u9019\u6a23\u4fbf\u65bc\u5f8c\u7e8c\u7684\u9664\u932f\u8207\u7dad\u8b77\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):FileNotFoundError\u662fPython\u4e2d\u7684\u4e00\u7a2e\u4f8b\u5916\u8655\u7406\uff0c\u7576\u7a0b\u5f0f\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\u6642\uff0c\u6703\u62cb\u51faFileNotFoundError\u4f8b\u5916\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528FileNotFoundError\u4f86\u8655\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u554f\u984c\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-3584","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-VO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3584","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=3584"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3584\/revisions"}],"predecessor-version":[{"id":12788,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3584\/revisions\/12788"}],"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=3584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}