{"id":1075522,"date":"2025-01-08T11:45:02","date_gmt":"2025-01-08T03:45:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1075522.html"},"modified":"2025-01-08T11:45:05","modified_gmt":"2025-01-08T03:45:05","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e6%96%87%e4%bb%b6%e5%a4%b9%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1075522.html","title":{"rendered":"python\u5982\u4f55\u67e5\u770b\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180552\/95c0e71d-76d8-46ef-903a-b5cc39248de0.webp\" alt=\"python\u5982\u4f55\u67e5\u770b\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\" \/><\/p>\n<p><p> <strong>Python\u67e5\u770b\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001try-except\u8bed\u53e5\u7b49\uff0cos.path.exists()\u65b9\u6cd5\u3001os.path.isdir()\u65b9\u6cd5\u3001pathlib.Path.exists()\u65b9\u6cd5<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528os.path.exists()\u65b9\u6cd5<\/strong>\uff1a\u8fd9\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u901a\u8fc7\u4f20\u9012\u6587\u4ef6\u5939\u8def\u5f84\u7ed9os.path.exists()\uff0c\u5982\u679c\u8def\u5f84\u5b58\u5728\uff0c\u65e0\u8bba\u662f\u6587\u4ef6\u8fd8\u662f\u6587\u4ef6\u5939\uff0c\u8be5\u65b9\u6cd5\u90fd\u4f1a\u8fd4\u56deTrue\u3002\u8981\u68c0\u67e5\u662f\u5426\u662f\u6587\u4ef6\u5939\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408os.path.isdir()\u65b9\u6cd5\u8fdb\u4e00\u6b65\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def check_directory_existence(path):<\/p>\n<p>    if os.path.exists(path):<\/p>\n<p>        if os.path.isdir(path):<\/p>\n<p>            print(f&quot;The directory &#39;{path}&#39; exists.&quot;)<\/p>\n<p>        else:<\/p>\n<p>            print(f&quot;The path &#39;{path}&#39; exists, but it is not a directory.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The directory &#39;{path}&#39; does not exist.&quot;)<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>check_directory_existence(&#39;\/path\/to\/directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u591a\u79cd\u65b9\u6cd5\u3002\u901a\u8fc7os\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528os.path.exists()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>os.path.exists()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u68c0\u67e5\u7ed9\u5b9a\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u65e0\u8bba\u662f\u6587\u4ef6\u8fd8\u662f\u6587\u4ef6\u5939\uff0c\u53ea\u8981\u8def\u5f84\u5b58\u5728\uff0c\u8be5\u65b9\u6cd5\u90fd\u4f1a\u8fd4\u56deTrue\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def check_path_exists(path):<\/p>\n<p>    if os.path.exists(path):<\/p>\n<p>        print(f&quot;The path &#39;{path}&#39; exists.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The path &#39;{path}&#39; does not exist.&quot;)<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>check_path_exists(&#39;\/path\/to\/directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528os.path.isdir()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>os.path.isdir()<\/code>\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u786e\u8ba4\u8def\u5f84\u662f\u5426\u662f\u4e00\u4e2a\u6587\u4ef6\u5939\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u51c6\u786e\u5730\u5224\u65ad\u76ee\u6807\u8def\u5f84\u662f\u5426\u662f\u4e00\u4e2a\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def check_directory(path):<\/p>\n<p>    if os.path.isdir(path):<\/p>\n<p>        print(f&quot;The directory &#39;{path}&#39; exists.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The directory &#39;{path}&#39; does not exist.&quot;)<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>check_directory(&#39;\/path\/to\/directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u3002\u5b83\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528pathlib.Path.exists()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>pathlib.Path.exists()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u4e0e<code>os.path.exists()<\/code>\u7c7b\u4f3c\uff0c\u8be5\u65b9\u6cd5\u4f1a\u8fd4\u56deTrue\u6216False\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def check_path_exists(path):<\/p>\n<p>    p = Path(path)<\/p>\n<p>    if p.exists():<\/p>\n<p>        print(f&quot;The path &#39;{path}&#39; exists.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The path &#39;{path}&#39; does not exist.&quot;)<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>check_path_exists(&#39;\/path\/to\/directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528pathlib.Path.is_dir()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>pathlib.Path.is_dir()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u68c0\u67e5\u8def\u5f84\u662f\u5426\u662f\u4e00\u4e2a\u6587\u4ef6\u5939\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u52a0\u51c6\u786e\u5730\u5224\u65ad\u76ee\u6807\u8def\u5f84\u662f\u5426\u4e3a\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def check_directory(path):<\/p>\n<p>    p = Path(path)<\/p>\n<p>    if p.is_dir():<\/p>\n<p>        print(f&quot;The directory &#39;{path}&#39; exists.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The directory &#39;{path}&#39; does not exist.&quot;)<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>check_directory(&#39;\/path\/to\/directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528try-except\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\uff0c\u901a\u8fc7\u5c1d\u8bd5\u8bbf\u95ee\u76ee\u5f55\u6765\u5224\u65ad\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def check_directory_with_exception(path):<\/p>\n<p>    try:<\/p>\n<p>        os.listdir(path)<\/p>\n<p>        print(f&quot;The directory &#39;{path}&#39; exists.&quot;)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;The directory &#39;{path}&#39; does not exist.&quot;)<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>check_directory_with_exception(&#39;\/path\/to\/directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u8003\u8651\u5230\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u683c\u5f0f\u5dee\u5f02\u3002\u4f8b\u5982\uff0c\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u8def\u5f84\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\uff0c\u800c\u5728Linux\u548cMac\u7cfb\u7edf\u4e2d\uff0c\u8def\u5f84\u4f7f\u7528\u6b63\u659c\u6760\uff08\/\uff09\u3002\u4e3a\u4e86\u4fdd\u8bc1\u4ee3\u7801\u7684\u517c\u5bb9\u6027\uff0c\u5efa\u8bae\u4f7f\u7528<code>os.path.join()<\/code>\u65b9\u6cd5\u6765\u6784\u5efa\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def check_directory_cross_platform(base_path, folder_name):<\/p>\n<p>    path = os.path.join(base_path, folder_name)<\/p>\n<p>    if os.path.isdir(path):<\/p>\n<p>        print(f&quot;The directory &#39;{path}&#39; exists.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The directory &#39;{path}&#39; does not exist.&quot;)<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>check_directory_cross_platform(&#39;C:\\\\Users\\\\Username&#39;, &#39;Documents&#39;)  # Windows<\/p>\n<p>check_directory_cross_platform(&#39;\/home\/username&#39;, &#39;documents&#39;)  # Linux\/Mac<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u4f7f\u7528os\u548cpathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528os\u548cpathlib\u6a21\u5757\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u8def\u5f84\u64cd\u4f5c\u548c\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u5148\u4f7f\u7528os\u6a21\u5757\u5904\u7406\u8def\u5f84\u62fc\u63a5\uff0c\u518d\u4f7f\u7528pathlib\u6a21\u5757\u8fdb\u884c\u8def\u5f84\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<p>def combined_check_directory(base_path, folder_name):<\/p>\n<p>    path = os.path.join(base_path, folder_name)<\/p>\n<p>    p = Path(path)<\/p>\n<p>    if p.is_dir():<\/p>\n<p>        print(f&quot;The directory &#39;{path}&#39; exists.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The directory &#39;{path}&#39; does not exist.&quot;)<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>combined_check_directory(&#39;C:\\\\Users\\\\Username&#39;, &#39;Documents&#39;)  # Windows<\/p>\n<p>combined_check_directory(&#39;\/home\/username&#39;, &#39;documents&#39;)  # Linux\/Mac<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002<strong>\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001try-except\u8bed\u53e5\u7b49\u65b9\u6cd5\uff0c\u80fd\u591f\u7075\u6d3b\u5e94\u5bf9\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002<\/strong>\u65e0\u8bba\u662f\u7b80\u5355\u7684\u8def\u5f84\u5224\u65ad\uff0c\u8fd8\u662f\u590d\u6742\u7684\u8def\u5f84\u64cd\u4f5c\uff0c\u90fd\u53ef\u4ee5\u627e\u5230\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5224\u65ad\u4e00\u4e2a\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>path.exists()<\/code>\u6216<code>path.isdir()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>os.path.exists(&#39;\u6587\u4ef6\u5939\u8def\u5f84&#39;)<\/code>\u6216<code>os.path.isdir(&#39;\u6587\u4ef6\u5939\u8def\u5f84&#39;)<\/code>\u6765\u5224\u65ad\u3002\u8fd4\u56de\u503c\u4e3a<code>True<\/code>\u8868\u793a\u6587\u4ef6\u5939\u5b58\u5728\uff0c\u4e3a<code>False<\/code>\u8868\u793a\u4e0d\u5b58\u5728\u3002<\/p>\n<p><strong>\u5982\u679c\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\uff0c\u6211\u5e94\u8be5\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>makedirs()<\/code>\u6216<code>mkdir()<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u6587\u4ef6\u5939\u3002<code>os.makedirs(&#39;\u6587\u4ef6\u5939\u8def\u5f84&#39;)<\/code>\u53ef\u4ee5\u521b\u5efa\u591a\u5c42\u76ee\u5f55\uff0c\u800c<code>os.mkdir(&#39;\u6587\u4ef6\u5939\u8def\u5f84&#39;)<\/code>\u5219\u53ea\u80fd\u521b\u5efa\u5355\u5c42\u76ee\u5f55\u3002\u5728\u521b\u5efa\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u7528\u4e0a\u8ff0\u65b9\u6cd5\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5df2\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u5939\u5b58\u5728\u4f46\u65e0\u6cd5\u8bbf\u95ee\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u68c0\u67e5\u6587\u4ef6\u5939\u7684\u5b58\u5728\u6027\u65f6\uff0c\u5982\u679c\u6587\u4ef6\u5939\u5b58\u5728\u4f46\u7531\u4e8e\u6743\u9650\u95ee\u9898\u65e0\u6cd5\u8bbf\u95ee\uff0c\u53ef\u80fd\u4f1a\u5f15\u53d1<code>PermissionError<\/code>\u3002\u4e3a\u907f\u514d\u6b64\u7c7b\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5728\u6355\u83b7\u5230\u5f02\u5e38\u65f6\u63d0\u4f9b\u76f8\u5e94\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u6bd4\u5982\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u6216\u8bf7\u6c42\u7528\u6237\u66f4\u6539\u6587\u4ef6\u5939\u6743\u9650\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u67e5\u770b\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001try-except\u8bed\u53e5\u7b49\uff0cos.p [&hellip;]","protected":false},"author":3,"featured_media":1075528,"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\/1075522"}],"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=1075522"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075522\/revisions"}],"predecessor-version":[{"id":1075529,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075522\/revisions\/1075529"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1075528"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1075522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1075522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1075522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}