{"id":1114426,"date":"2025-01-08T17:57:08","date_gmt":"2025-01-08T09:57:08","guid":{"rendered":""},"modified":"2025-01-08T17:57:12","modified_gmt":"2025-01-08T09:57:12","slug":"python%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8b%e6%9c%ac%e5%9c%b0%e6%96%87%e4%bb%b6%e5%ad%98%e5%9c%a8%e4%b8%8d%e5%ad%98%e5%9c%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1114426.html","title":{"rendered":"python\u5982\u4f55\u68c0\u6d4b\u672c\u5730\u6587\u4ef6\u5b58\u5728\u4e0d\u5b58\u5728"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075629\/fa28f412-cdcc-43e0-937a-6e1fcdea928c.webp\" alt=\"python\u5982\u4f55\u68c0\u6d4b\u672c\u5730\u6587\u4ef6\u5b58\u5728\u4e0d\u5b58\u5728\" \/><\/p>\n<p><p> <strong>Python\u68c0\u6d4b\u672c\u5730\u6587\u4ef6\u5b58\u5728\u4e0e\u5426\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001try-except\u5757\u7b49\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h2>\n<\/p>\n<p><h3>1\u3001os.path.exists()<\/h3>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u591a\u79cd\u65b9\u6cd5\u3002<code>os.path.exists()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u68c0\u6d4b\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if os.path.exists(file_path):<\/p>\n<p>    print(f&#39;{file_path} \u5b58\u5728&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(f&#39;{file_path} \u4e0d\u5b58\u5728&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u68c0\u6d4b\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><h3>2\u3001os.path.isfile()<\/h3>\n<\/p>\n<p><p><code>os.path.isfile()<\/code>\u65b9\u6cd5\u4e13\u95e8\u7528\u4e8e\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if os.path.isfile(file_path):<\/p>\n<p>    print(f&#39;{file_path} \u662f\u4e00\u4e2a\u6587\u4ef6&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(f&#39;{file_path} \u4e0d\u662f\u4e00\u4e2a\u6587\u4ef6&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u53ea\u68c0\u6d4b\u6587\u4ef6\uff0c\u4e0d\u4f1a\u8bef\u5224\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h2>\n<\/p>\n<p><h3>1\u3001Path.exists()<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u8def\u5f84\u3002<code>Path.exists()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u68c0\u6d4b\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&#39;example.txt&#39;)<\/p>\n<p>if file_path.exists():<\/p>\n<p>    print(f&#39;{file_path} \u5b58\u5728&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(f&#39;{file_path} \u4e0d\u5b58\u5728&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u73b0\u4ee3\u5316\u7684\u8def\u5f84\u5904\u7406\u65b9\u6cd5\uff0c\u4ee3\u7801\u66f4\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><h3>2\u3001Path.is_file()<\/h3>\n<\/p>\n<p><p><code>Path.is_file()<\/code>\u65b9\u6cd5\u4e13\u95e8\u7528\u4e8e\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&#39;example.txt&#39;)<\/p>\n<p>if file_path.is_file():<\/p>\n<p>    print(f&#39;{file_path} \u662f\u4e00\u4e2a\u6587\u4ef6&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(f&#39;{file_path} \u4e0d\u662f\u4e00\u4e2a\u6587\u4ef6&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u53ea\u68c0\u6d4b\u6587\u4ef6\uff0c\u9002\u5408\u73b0\u4ee3Python\u4ee3\u7801\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528try-except\u5757<\/h2>\n<\/p>\n<p><h3>1\u3001\u6253\u5f00\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>try-except<\/code>\u5757\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u4f1a\u5f15\u53d1<code>FileNotFoundError<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;example.txt&#39;<\/p>\n<p>try:<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        print(f&#39;{file_path} \u5b58\u5728&#39;)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(f&#39;{file_path} \u4e0d\u5b58\u5728&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u53ef\u4ee5\u5728\u68c0\u6d4b\u6587\u4ef6\u5b58\u5728\u7684\u540c\u65f6\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528os.access()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>os.access()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u68c0\u67e5\u6587\u4ef6\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u5305\u62ec\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if os.access(file_path, os.F_OK):<\/p>\n<p>    print(f&#39;{file_path} \u5b58\u5728&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(f&#39;{file_path} \u4e0d\u5b58\u5728&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u53ef\u4ee5\u540c\u65f6\u68c0\u67e5\u6587\u4ef6\u7684\u8bfb\u5199\u6743\u9650\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ecos\u6a21\u5757\u3001pathlib\u6a21\u5757\u548ctry-except\u5757\u7b49\u3002<strong>os.path.exists()\u65b9\u6cd5\u9002\u5408\u68c0\u6d4b\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u5b58\u5728\uff0cos.path.isfile()\u548cpathlib.Path.is_file()\u65b9\u6cd5\u4e13\u95e8\u7528\u4e8e\u68c0\u6d4b\u6587\u4ef6\u7684\u5b58\u5728\uff0cpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u73b0\u4ee3\u5316\u7684\u8def\u5f84\u5904\u7406\u65b9\u6cd5\uff0ctry-except\u5757\u53ef\u4ee5\u5728\u68c0\u6d4b\u6587\u4ef6\u5b58\u5728\u7684\u540c\u65f6\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u7279\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>path.exists()<\/code>\u65b9\u6cd5\u6765\u68c0\u6d4b\u7279\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528<code>os.path.exists(path)<\/code>\uff0c\u5176\u4e2d<code>path<\/code>\u662f\u60a8\u60f3\u8981\u68c0\u67e5\u7684\u6587\u4ef6\u8def\u5f84\u3002\u5982\u679c\u8fd4\u56de\u503c\u4e3a<code>True<\/code>\uff0c\u8868\u793a\u6587\u4ef6\u5b58\u5728\uff1b\u5982\u679c\u8fd4\u56de\u503c\u4e3a<code>False<\/code>\uff0c\u5219\u6587\u4ef6\u4e0d\u5b58\u5728\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u6211\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5982\u679c\u5728\u68c0\u67e5\u6587\u4ef6\u5b58\u5728\u6027\u65f6\u53d1\u73b0\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u91c7\u53d6\u4e0d\u540c\u7684\u5904\u7406\u63aa\u65bd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u9009\u62e9\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u3001\u6253\u5370\u4e00\u6761\u9519\u8bef\u6d88\u606f\uff0c\u6216\u8005\u6267\u884c\u5176\u4ed6\u903b\u8f91\u3002\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\uff0c\u4ece\u800c\u4f18\u96c5\u5730\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u5b58\u5728\u65f6\uff0c\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>os.path.exists()<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u6765\u68c0\u67e5\u6587\u4ef6\u7684\u5b58\u5728\u6027\u3002<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\uff0c\u4f7f\u7528<code>Path(path).exists()<\/code>\u540c\u6837\u80fd\u6709\u6548\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u8fd9\u79cd\u65b9\u6cd5\u66f4\u52a0\u73b0\u4ee3\u5316\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u66f4\u9ad8\uff0c\u4e14\u4e0ePython\u7684\u5176\u4ed6\u529f\u80fd\u517c\u5bb9\u6027\u66f4\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u68c0\u6d4b\u672c\u5730\u6587\u4ef6\u5b58\u5728\u4e0e\u5426\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001try-except\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1114437,"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\/1114426"}],"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=1114426"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114426\/revisions"}],"predecessor-version":[{"id":1114439,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114426\/revisions\/1114439"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1114437"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1114426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1114426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1114426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}