{"id":3580,"date":"2025-06-01T14:58:43","date_gmt":"2025-06-04T07:51:43","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3580"},"modified":"2025-06-04T14:58:43","modified_gmt":"2025-06-04T07:51:43","slug":"%e8%a7%a3%e6%b1%bapython%e7%a8%8b%e5%bc%8f%e7%a2%bc%e4%b8%ad%e7%9a%84environmenterror","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e8%a7%a3%e6%b1%bapython%e7%a8%8b%e5%bc%8f%e7%a2%bc%e4%b8%ad%e7%9a%84environmenterror\/","title":{"rendered":"\u5168\u9762\u89e3\u6790 Python \u7684 EnvironmentError\uff1a\u5982\u4f55\u6709\u6548\u6355\u6349\u8207\u8655\u7406"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, EnvironmentError, \u6355\u6349\u4f8b\u5916, \u932f\u8aa4\u8655\u7406\"><\/p>\n<h1>\u5168\u9762\u89e3\u6790 Python \u7684 EnvironmentError\uff1a\u5982\u4f55\u6709\u6548\u6355\u6349\u8207\u8655\u7406<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`EnvironmentError` \u662f\u4e00\u500b\u6a19\u6e96\u7684\u4f8b\u5916\uff0c\u8868\u793a\u67d0\u4e9b\u64cd\u4f5c\u56e0\u8207\u64cd\u4f5c\u7cfb\u7d71\u7684\u74b0\u5883\u76f8\u95dc\u800c\u5931\u6557\u3002\u9019\u985e\u932f\u8aa4\u901a\u5e38\u5305\u62ec\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6b0a\u9650\u4e0d\u8db3\u7b49\u554f\u984c\u3002\u96a8\u8457 Python \u7684\u66f4\u65b0\uff0c\u5f9e Python 3.3 \u958b\u59cb\uff0c`EnvironmentError` \u88ab\u6574\u5408\u5230\u4e86 `OSError` \u4e2d\uff0c\u56e0\u6b64\u5728\u73fe\u4ee3Python\u7a0b\u5f0f\u4e2d\uff0c\u4f7f\u7528 `OSError` \u4f86\u6355\u6349\u9019\u985e\u932f\u8aa4\u66f4\u52a0\u7b26\u5408\u7576\u524d\u7684\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u95dc\u65bc `OSError` \u7684\u91cd\u8981\u8cc7\u8a0a\uff1a<\/p>\n<p>&#8211; **OSError** \u662f `EnvironmentError` \u7684\u5b50\u985e\uff0c\u8868\u793a\u64cd\u4f5c\u7cfb\u7d71\u7684\u932f\u8aa4\u3002<br \/>\n&#8211; `errno` \u5c6c\u6027\u662f\u4e00\u500b\u6574\u6578\uff0c\u4ee3\u8868\u64cd\u4f5c\u7cfb\u7d71\u7684\u932f\u8aa4\u4ee3\u78bc\u3002<br \/>\n&#8211; `strerror` \u5c6c\u6027\u662f\u4e00\u500b\u5b57\u7b26\u4e32\uff0c\u63d0\u4f9b\u932f\u8aa4\u7684\u8a73\u7d30\u63cf\u8ff0\u3002<\/p>\n<p>### \u6355\u6349 OSError \u7684\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u6355\u6349 `OSError`\uff08\u539f `EnvironmentError`\uff09\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntry:<br \/>\n    f = open(&#8216;foo.txt&#8217;)<br \/>\nexcept OSError as e:<br \/>\n    print(&#8216;Error:&#8217;, e.errno, e.strerror)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5617\u8a66\u6253\u958b\u4e00\u500b\u6587\u4ef6\uff0c\u82e5\u8a72\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5c07\u62cb\u51fa `OSError`\uff0c\u6211\u5011\u53ef\u4ee5\u6355\u6349\u5b83\u4e26\u6253\u5370\u51fa\u932f\u8aa4\u4ee3\u78bc\u548c\u63cf\u8ff0\u3002<\/p>\n<p>### OSError \u7684\u61c9\u7528<\/p>\n<p>`OSError` \u53ef\u4ee5\u7528\u65bc\u591a\u7a2e\u60c5\u5883\uff0c\u4f8b\u5982\u6aa2\u67e5\u6587\u4ef6\u6216\u76ee\u9304\u7684\u5b58\u5728\u6027\u53ca\u5176\u8a2a\u554f\u6b0a\u9650\uff1a<\/p>\n<p>#### \u6aa2\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/p>\n<p>&#8220;`python<br \/>\nimport os<\/p>\n<p>try:<br \/>\n    os.stat(&#8216;foo.txt&#8217;)<br \/>\nexcept OSError as e:<br \/>\n    print(&#8216;File does not exist:&#8217;, e.strerror)<br \/>\n&#8220;`<\/p>\n<p>#### \u6aa2\u67e5\u6587\u4ef6\u662f\u5426\u53ef\u8b80<\/p>\n<p>&#8220;`python<br \/>\nimport os<\/p>\n<p>try:<br \/>\n    f = open(&#8216;foo.txt&#8217;, &#8216;r&#8217;)<br \/>\nexcept OSError as e:<br \/>\n    print(&#8216;File cannot be read:&#8217;, e.strerror)<br \/>\n&#8220;`<\/p>\n<p>#### \u6aa2\u67e5\u6587\u4ef6\u662f\u5426\u53ef\u5beb<\/p>\n<p>&#8220;`python<br \/>\nimport os<\/p>\n<p>try:<br \/>\n    f = open(&#8216;foo.txt&#8217;, &#8216;w&#8217;)<br \/>\nexcept OSError as e:<br \/>\n    print(&#8216;File cannot be written:&#8217;, e.strerror)<br \/>\n&#8220;`<\/p>\n<p>#### \u6aa2\u67e5\u76ee\u9304\u662f\u5426\u5b58\u5728<\/p>\n<p>&#8220;`python<br \/>\nimport os<\/p>\n<p>try:<br \/>\n    os.stat(&#8216;foo&#8217;)<br \/>\nexcept OSError as e:<br \/>\n    print(&#8216;Directory does not exist:&#8217;, e.strerror)<br \/>\n&#8220;`<\/p>\n<p>### \u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/p>\n<p>\u5728\u8655\u7406 `OSError` \u6642\uff0c\u5efa\u8b70\u4f7f\u7528\u66f4\u5177\u9ad4\u7684\u932f\u8aa4\u985e\u578b\u4f86\u6355\u6349\u7279\u5b9a\u7684\u932f\u8aa4\uff0c\u5982 `FileNotFoundError` \u6216 `PermissionError`\u3002\u9019\u6a23\u53ef\u4ee5\u63d0\u4f9b\u66f4\u8a73\u7d30\u7684\u932f\u8aa4\u4fe1\u606f\uff0c\u8b93\u7528\u6236\u66f4\u5bb9\u6613\u7406\u89e3\u554f\u984c\u6240\u5728\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u82e5\u60a8\u60f3\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u932f\u8aa4\u8655\u7406\u6a5f\u5236\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/63bcf5b5fd89780001b5fbe3)\uff0c\u5167\u5bb9\u6db5\u84cb\u4e86\u66f4\u591a\u4f8b\u5916\u8655\u7406\u7684\u6700\u4f73\u5be6\u8e10\u8207\u793a\u7bc4\u3002<\/p>\n<p>### \u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/p>\n<p>**Q1: \u5982\u4f55\u77e5\u9053\u6355\u6349\u5230\u7684 OSError \u662f\u4ec0\u9ebc\u985e\u578b\u7684\u932f\u8aa4\uff1f**<br \/>\nA1: \u60a8\u53ef\u4ee5\u4f7f\u7528 `type(e)` \u4f86\u7372\u53d6\u932f\u8aa4\u7684\u5177\u9ad4\u985e\u578b\uff0c\u4e26\u6839\u64da\u9700\u8981\u9032\u884c\u76f8\u61c9\u8655\u7406\u3002<\/p>\n<p>**Q2: \u6211\u53ef\u4ee5\u6355\u6349\u591a\u7a2e\u932f\u8aa4\u985e\u578b\u55ce\uff1f**<br \/>\nA2: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u500b `except` \u5340\u584a\u4f86\u6355\u6349\u4e0d\u540c\u985e\u578b\u7684\u932f\u8aa4\uff0c\u4ee5\u4fbf\u65bc\u5be6\u73fe\u7279\u5b9a\u7684\u932f\u8aa4\u8655\u7406\u908f\u8f2f\u3002<\/p>\n<p>**Q3: \u5728\u4f55\u7a2e\u60c5\u6cc1\u4e0b\u61c9\u4f7f\u7528 OSError \u800c\u4e0d\u662f\u5176\u4ed6\u81ea\u5b9a\u7fa9\u4f8b\u5916\uff1f**<br \/>\nA3: \u7576\u60a8\u9700\u8981\u8655\u7406\u8207\u64cd\u4f5c\u7cfb\u7d71\u76f8\u95dc\u7684\u932f\u8aa4\u6642\uff0c\u4f7f\u7528 `OSError` \u662f\u6700\u4f73\u9078\u64c7\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u6355\u6349\u8a31\u591a\u8207\u6587\u4ef6\u548c\u76ee\u9304\u64cd\u4f5c\u76f8\u95dc\u7684\u932f\u8aa4\u3002<\/p>\n<p>\u7e3d\u4e4b\uff0c\u7406\u89e3\u4e26\u6709\u6548\u6355\u6349 `OSError` \u662f\u63d0\u5347 Python \u7a0b\u5f0f\u5065\u58ef\u6027\u7684\u91cd\u8981\u6b65\u9a5f\uff0c\u9019\u4e0d\u50c5\u80fd\u5e6b\u52a9\u6211\u5011\u66f4\u597d\u5730\u8655\u7406\u932f\u8aa4\uff0c\u9084\u53ef\u4ee5\u70ba\u7528\u6236\u63d0\u4f9b\u66f4\u53cb\u597d\u7684\u932f\u8aa4\u63d0\u793a\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u89e3\u6c7aPython\u7a0b\u5f0f\u78bc\u4e2d\u7684EnvironmentError\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u6709\u7528\u7684\u6280\u5de7\uff0c\u4ee5\u4fbf\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u89e3\u6c7a\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-3580","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-VK","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3580","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=3580"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3580\/revisions"}],"predecessor-version":[{"id":12790,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3580\/revisions\/12790"}],"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=3580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}