{"id":1068150,"date":"2024-12-31T16:40:50","date_gmt":"2024-12-31T08:40:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068150.html"},"modified":"2024-12-31T16:40:52","modified_gmt":"2024-12-31T08:40:52","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e5%90%8c%e5%90%8d%e7%9b%ae%e5%bd%95%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068150.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u67e5\u770b\u540c\u540d\u76ee\u5f55\u662f\u5426\u5b58\u5728"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/9f587b38-36ed-4bdd-b8c8-fe1e4407eb84.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u67e5\u770b\u540c\u540d\u76ee\u5f55\u662f\u5426\u5b58\u5728\" \/><\/p>\n<blockquote>\n<p><p>search(&quot;python\u4e2d\u5982\u4f55\u67e5\u770b\u540c\u540d\u76ee\u5f55\u662f\u5426\u5b58\u5728&quot;)<\/p>\n<\/p>\n<\/blockquote>\n<p><p><strong>\u5728 Python \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u7684 <code>os.path.isdir()<\/code> \u51fd\u6570\u6216 <code>pathlib<\/code> \u6a21\u5757\u7684 <code>Path.is_dir()<\/code> \u65b9\u6cd5\u6765\u68c0\u67e5\u6307\u5b9a\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>directory_path = &#39;\/path\/to\/directory&#39;<\/p>\n<p>if os.path.isdir(directory_path):<\/p>\n<p>    print(f&quot;\u76ee\u5f55 &#39;{directory_path}&#39; \u5b58\u5728\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u76ee\u5f55 &#39;{directory_path}&#39; \u4e0d\u5b58\u5728\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>directory_path = Path(&#39;\/path\/to\/directory&#39;)<\/p>\n<p>if directory_path.is_dir():<\/p>\n<p>    print(f&quot;\u76ee\u5f55 &#39;{directory_path}&#39; \u5b58\u5728\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u76ee\u5f55 &#39;{directory_path}&#39; \u4e0d\u5b58\u5728\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5176\u4e2d\uff0c<code>os.path.isdir()<\/code> \u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u6307\u5b9a\u8def\u5f84\u662f\u5426\u4e3a\u76ee\u5f55\uff0c\u800c <code>Path.is_dir()<\/code> \u65b9\u6cd5\u662f <code>pathlib<\/code> \u6a21\u5757\u63d0\u4f9b\u7684\u9762\u5411\u5bf9\u8c61\u65b9\u5f0f\uff0c\u7528\u4e8e\u6267\u884c\u76f8\u540c\u7684\u68c0\u67e5\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u8bf7\u6ce8\u610f\uff0c<code>os.path.exists()<\/code> \u51fd\u6570\u53ef\u4ee5\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u4f46\u65e0\u6cd5\u533a\u5206\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>path = &#39;\/path\/to\/path&#39;<\/p>\n<p>if os.path.exists(path):<\/p>\n<p>    if os.path.isdir(path):<\/p>\n<p>        print(f&quot;&#39;{path}&#39; \u662f\u4e00\u4e2a\u76ee\u5f55\u3002&quot;)<\/p>\n<p>    elif os.path.isfile(path):<\/p>\n<p>        print(f&quot;&#39;{path}&#39; \u662f\u4e00\u4e2a\u6587\u4ef6\u3002&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;&#39;{path}&#39; \u5b58\u5728\uff0c\u4f46\u65e2\u4e0d\u662f\u6587\u4ef6\u4e5f\u4e0d\u662f\u76ee\u5f55\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;&#39;{path}&#39; \u4e0d\u5b58\u5728\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5efa\u8bae\u4f7f\u7528 <code>os.path.isdir()<\/code> \u6216 <code>Path.is_dir()<\/code> \u6765\u51c6\u786e\u5224\u65ad\u76ee\u5f55\u7684\u5b58\u5728\u6027\uff0c\u4ee5\u907f\u514d\u8bef\u5224\u6587\u4ef6\u4e3a\u76ee\u5f55\u7684\u60c5\u51b5\u3002<\/strong> <\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u68c0\u67e5\u7279\u5b9a\u76ee\u5f55\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>exists()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u7279\u5b9a\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\ndirectory = &#39;your_directory_name&#39;\nif os.path.exists(directory):\n    print(f&quot;\u76ee\u5f55 &#39;{directory}&#39; \u5b58\u5728\u3002&quot;)\nelse:\n    print(f&quot;\u76ee\u5f55 &#39;{directory}&#39; \u4e0d\u5b58\u5728\u3002&quot;)\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u9002\u7528\u4e8e\u68c0\u67e5\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u5b58\u5728\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5217\u51fa\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.listdir()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u7684\u540d\u79f0\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\ncurrent_directory = &#39;.&#39;\nitems = os.listdir(current_directory)\nprint(&quot;\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\uff1a&quot;, items)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4e86\u89e3\u5f53\u524d\u76ee\u5f55\u7684\u7ed3\u6784\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u8def\u5f84\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55\uff1f<\/strong><br \/>\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>isfile()<\/code>\u548c<code>isdir()<\/code>\u51fd\u6570\u53ef\u4ee5\u5206\u522b\u5224\u65ad\u7ed9\u5b9a\u8def\u5f84\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\npath = &#39;your_path_here&#39;\nif os.path.isfile(path):\n    print(f&quot;\u8def\u5f84 &#39;{path}&#39; \u662f\u4e00\u4e2a\u6587\u4ef6\u3002&quot;)\nelif os.path.isdir(path):\n    print(f&quot;\u8def\u5f84 &#39;{path}&#39; \u662f\u4e00\u4e2a\u76ee\u5f55\u3002&quot;)\nelse:\n    print(f&quot;\u8def\u5f84 &#39;{path}&#39; \u4e0d\u5b58\u5728\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u8ba9\u4f60\u53ef\u4ee5\u66f4\u7ec6\u81f4\u5730\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u4e0d\u540c\u7c7b\u578b\u7684\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"search(&quot;python\u4e2d\u5982\u4f55\u67e5\u770b\u540c\u540d\u76ee\u5f55\u662f\u5426\u5b58\u5728&quot;) \u5728 Python \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1068154,"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\/1068150"}],"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=1068150"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068150\/revisions"}],"predecessor-version":[{"id":1068155,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068150\/revisions\/1068155"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1068154"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1068150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1068150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1068150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}