{"id":1039347,"date":"2024-12-31T12:31:37","date_gmt":"2024-12-31T04:31:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1039347.html"},"modified":"2024-12-31T12:31:40","modified_gmt":"2024-12-31T04:31:40","slug":"python%e5%a6%82%e4%bd%95%e6%a0%a1%e9%aa%8c%e6%96%87%e4%bb%b6%e5%a4%b9%e5%ad%98%e5%9c%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1039347.html","title":{"rendered":"python\u5982\u4f55\u6821\u9a8c\u6587\u4ef6\u5939\u5b58\u5728"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/2ab981f6-4030-4285-92fe-836a2cde6488.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u6821\u9a8c\u6587\u4ef6\u5939\u5b58\u5728\" \/><\/p>\n<p><p> <strong>Python\u6821\u9a8c\u6587\u4ef6\u5939\u5b58\u5728\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u7b49\uff0c\u5e38\u7528\u65b9\u6cd5\u6709\uff1aos.path.exists()\u3001os.path.isdir()\u3001pathlib.Path.exists()\u3002<\/strong>\u5176\u4e2d\uff0cos\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u7840\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u529f\u80fd\uff0c\u800cpathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u66f4\u73b0\u4ee3\u5316\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u6a21\u5757\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u4ee5\u53ca\u76f8\u5e94\u7684\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001os.path.exists()<\/h4>\n<\/p>\n<p><p>os.path.exists() \u662f\u68c0\u67e5\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728\u7684\u4e00\u4e2a\u901a\u7528\u65b9\u6cd5\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u6307\u5b9a\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = &#39;\/path\/to\/directory&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(directory):<\/p>\n<p>    print(f&quot;The directory {directory} exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The directory {directory} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001os.path.isdir()<\/h4>\n<\/p>\n<p><p>os.path.isdir() \u7528\u4e8e\u68c0\u67e5\u6307\u5b9a\u8def\u5f84\u662f\u5426\u4e3a\u76ee\u5f55\u3002\u5982\u679c\u8def\u5f84\u5b58\u5728\u4e14\u4e3a\u76ee\u5f55\uff0c\u5219\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = &#39;\/path\/to\/directory&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u4e3a\u76ee\u5f55<\/strong><\/h2>\n<p>if os.path.isdir(directory):<\/p>\n<p>    print(f&quot;The directory {directory} exists and is a directory.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The directory {directory} does not exist or is not a directory.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001pathlib.Path.exists()<\/h4>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\u3002Path.exists() \u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = Path(&#39;\/path\/to\/directory&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if directory.exists():<\/p>\n<p>    print(f&quot;The directory {directory} exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The directory {directory} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001pathlib.Path.is_dir()<\/h4>\n<\/p>\n<p><p>Path.is_dir() \u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u8def\u5f84\u662f\u5426\u4e3a\u76ee\u5f55\u3002\u5982\u679c\u8def\u5f84\u5b58\u5728\u4e14\u4e3a\u76ee\u5f55\uff0c\u5219\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = Path(&#39;\/path\/to\/directory&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u4e3a\u76ee\u5f55<\/strong><\/h2>\n<p>if directory.is_dir():<\/p>\n<p>    print(f&quot;The directory {directory} exists and is a directory.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The directory {directory} does not exist or is not a directory.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u5f02\u5e38\u5904\u7406\u6765\u5904\u7406\u8def\u5f84\u68c0\u67e5\u8fc7\u7a0b\u4e2d\u7684\u6f5c\u5728\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5f53\u8def\u5f84\u4e2d\u5305\u542b\u65e0\u6548\u5b57\u7b26\u65f6\uff0c\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = &#39;\/path\/to\/directory&#39;<\/p>\n<p>try:<\/p>\n<p>    # \u68c0\u67e5\u8def\u5f84\u662f\u5426\u4e3a\u76ee\u5f55<\/p>\n<p>    if os.path.isdir(directory):<\/p>\n<p>        print(f&quot;The directory {directory} exists and is a directory.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The directory {directory} does not exist or is not a directory.&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8de8\u5e73\u53f0\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>Python\u7684os\u6a21\u5757\u548cpathlib\u6a21\u5757\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\u90fd\u80fd\u5f88\u597d\u5730\u8de8\u5e73\u53f0\u5de5\u4f5c\u3002\u7136\u800c\uff0c\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u8def\u5f84\u8868\u793a\u6cd5\u4e0d\u540c\uff0c\u4f8b\u5982Windows\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\uff0c\u800cUnix\/Linux\u548cmacOS\u4f7f\u7528\u6b63\u659c\u6760\uff08\/\uff09\u3002pathlib\u6a21\u5757\u5728\u5904\u7406\u8def\u5f84\u65f6\uff0c\u80fd\u81ea\u52a8\u9002\u5e94\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u8868\u793a\u6cd5\uff0c\u56e0\u6b64\u5728\u7f16\u5199\u8de8\u5e73\u53f0\u4ee3\u7801\u65f6\uff0c\u63a8\u8350\u4f7f\u7528pathlib\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = Path(&#39;\/path\/to\/directory&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if directory.exists():<\/p>\n<p>    print(f&quot;The directory {directory} exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The directory {directory} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5728\u5f00\u53d1\u811a\u672c\u6216\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u68c0\u67e5\u914d\u7f6e\u6587\u4ef6\u5939\u3001\u65e5\u5fd7\u6587\u4ef6\u5939\u3001\u6570\u636e\u6587\u4ef6\u5939\u7b49\u662f\u5426\u5b58\u5728\uff0c\u5e76\u5728\u5fc5\u8981\u65f6\u8fdb\u884c\u521b\u5efa\u6216\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528os.makedirs() \u6216 Path.mkdir() \u65b9\u6cd5\u521b\u5efa\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = &#39;\/path\/to\/directory&#39;<\/p>\n<h2><strong>\u5982\u679c\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u76ee\u5f55<\/strong><\/h2>\n<p>if not os.path.exists(directory):<\/p>\n<p>    os.makedirs(directory)<\/p>\n<p>    print(f&quot;The directory {directory} has been created.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The directory {directory} already exists.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = Path(&#39;\/path\/to\/directory&#39;)<\/p>\n<h2><strong>\u5982\u679c\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u76ee\u5f55<\/strong><\/h2>\n<p>if not directory.exists():<\/p>\n<p>    directory.mkdir(parents=True)<\/p>\n<p>    print(f&quot;The directory {directory} has been created.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The directory {directory} already exists.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868\u6216\u8fdb\u884c\u5176\u4ed6\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = &#39;\/path\/to\/directory&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.isdir(directory):<\/p>\n<p>    # \u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868<\/p>\n<p>    files = os.listdir(directory)<\/p>\n<p>    print(f&quot;Files in directory {directory}: {files}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The directory {directory} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = Path(&#39;\/path\/to\/directory&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if directory.is_dir():<\/p>\n<p>    # \u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868<\/p>\n<p>    files = list(directory.iterdir())<\/p>\n<p>    print(f&quot;Files in directory {directory}: {files}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The directory {directory} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6027\u80fd\u548c\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u5939\u68c0\u67e5\u548c\u5176\u4ed6\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u8003\u8651\u6027\u80fd\u548c\u6700\u4f73\u5b9e\u8df5\u662f\u5f88\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528pathlib\u6a21\u5757<\/strong>\uff1apathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u5316\u548c\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u63a8\u8350\u5728Python 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u91cd\u590d\u68c0\u67e5<\/strong>\uff1a\u5728\u540c\u4e00\u4e2a\u51fd\u6570\u6216\u4ee3\u7801\u5757\u4e2d\uff0c\u907f\u514d\u91cd\u590d\u68c0\u67e5\u540c\u4e00\u4e2a\u8def\u5f84\u7684\u5b58\u5728\u6027\uff0c\u5c3d\u91cf\u5c06\u68c0\u67e5\u7ed3\u679c\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5f02\u5e38<\/strong>\uff1a\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u59cb\u7ec8\u8003\u8651\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u5e76\u4f7f\u7528try-except\u5757\u8fdb\u884c\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/strong>\uff1a\u5728\u7f16\u5199\u8de8\u5e73\u53f0\u4ee3\u7801\u65f6\uff0c\u4f7f\u7528pathlib\u6a21\u5757\u53ef\u4ee5\u81ea\u52a8\u9002\u5e94\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u8868\u793a\u6cd5\uff0c\u51cf\u5c11\u5e73\u53f0\u76f8\u5173\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = Path(&#39;\/path\/to\/directory&#39;)<\/p>\n<p>try:<\/p>\n<p>    # \u68c0\u67e5\u8def\u5f84\u662f\u5426\u4e3a\u76ee\u5f55<\/p>\n<p>    if directory.is_dir():<\/p>\n<p>        # \u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868<\/p>\n<p>        files = list(directory.iterdir())<\/p>\n<p>        print(f&quot;Files in directory {directory}: {files}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The directory {directory} does not exist.&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6821\u9a8c\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff0c\u5305\u62ecos\u6a21\u5757\u548cpathlib\u6a21\u5757\u3002<strong>os.path.exists() \u548c os.path.isdir() \u662f\u5e38\u7528\u7684\u68c0\u67e5\u65b9\u6cd5\uff0c\u800c pathlib.Path.exists() \u548c pathlib.Path.is_dir() \u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u5316\u548c\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u5f02\u5e38\u5904\u7406\u548c\u8def\u5f84\u64cd\u4f5c\u529f\u80fd\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u3002\u901a\u8fc7\u9075\u5faa\u6027\u80fd\u548c\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u66f4\u9ad8\u6548\u548c\u5065\u58ee\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>os.path.exists()<\/code>\u6216<code>os.path.isdir()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u5e03\u5c14\u503c\uff0c\u6307\u793a\u6307\u5b9a\u8def\u5f84\u662f\u5426\u5b58\u5728\u4ee5\u53ca\u662f\u5426\u4e3a\u76ee\u5f55\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_path = &#39;your_directory_path&#39;\nif os.path.exists(folder_path) and os.path.isdir(folder_path):\n    print(&quot;\u6587\u4ef6\u5939\u5b58\u5728&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u5939\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\uff0c\u6211\u8be5\u5982\u4f55\u521b\u5efa\u5b83\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.makedirs()<\/code>\u51fd\u6570\u521b\u5efa\u6587\u4ef6\u5939\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u591a\u5c42\u76ee\u5f55\u3002\u5982\u679c\u6307\u5b9a\u7684\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u4e0d\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_path = &#39;your_directory_path&#39;\nif not os.path.exists(folder_path):\n    os.makedirs(folder_path)\n    print(&quot;\u6587\u4ef6\u5939\u5df2\u521b\u5efa&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u5939\u5df2\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u68c0\u67e5\u6587\u4ef6\u5939\u7684\u6743\u9650\u6709\u4ec0\u4e48\u65b9\u6cd5\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.access()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u6587\u4ef6\u5939\u7684\u6743\u9650\u3002\u901a\u8fc7\u4f20\u5165\u8def\u5f84\u548c\u6743\u9650\u6807\u5fd7\uff0c\u53ef\u4ee5\u786e\u5b9a\u5f53\u524d\u7528\u6237\u662f\u5426\u53ef\u4ee5\u8bfb\u53d6\u3001\u5199\u5165\u6216\u6267\u884c\u8be5\u76ee\u5f55\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_path = &#39;your_directory_path&#39;\nif os.access(folder_path, os.R_OK):\n    print(&quot;\u60a8\u6709\u6743\u9650\u8bfb\u53d6\u8be5\u6587\u4ef6\u5939&quot;)\nelse:\n    print(&quot;\u60a8\u6ca1\u6709\u6743\u9650\u8bfb\u53d6\u8be5\u6587\u4ef6\u5939&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6709\u6548\u5730\u7ba1\u7406\u548c\u68c0\u67e5\u6587\u4ef6\u5939\u7684\u5b58\u5728\u4e0e\u6743\u9650\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6821\u9a8c\u6587\u4ef6\u5939\u5b58\u5728\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u7b49\uff0c\u5e38\u7528\u65b9\u6cd5\u6709\uff1aos.path.e [&hellip;]","protected":false},"author":3,"featured_media":1039356,"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\/1039347"}],"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=1039347"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039347\/revisions"}],"predecessor-version":[{"id":1039360,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039347\/revisions\/1039360"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1039356"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1039347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1039347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1039347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}