{"id":1063733,"date":"2024-12-31T16:03:05","date_gmt":"2024-12-31T08:03:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1063733.html"},"modified":"2024-12-31T16:03:07","modified_gmt":"2024-12-31T08:03:07","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e6%96%87%e4%bb%b6%e5%a4%b9%e5%95%8a","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1063733.html","title":{"rendered":"Python\u5982\u4f55\u521b\u5efa\u6587\u4ef6\u5939\u554a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/8dc5f324-b5db-4fa6-873d-8357bdd2ec5f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u521b\u5efa\u6587\u4ef6\u5939\u554a\" \/><\/p>\n<p><p> <strong>Python\u521b\u5efa\u6587\u4ef6\u5939\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001shutil\u6a21\u5757\u3002\u6700\u5e38\u7528\u4e14\u63a8\u8350\u7684\u662f\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e24\u79cd\u65b9\u5f0f\uff0c\u5e76\u89e3\u91ca\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757\u521b\u5efa\u6587\u4ef6\u5939<\/p>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\uff0c\u5305\u62ec\u521b\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7b49\u3002\u4f7f\u7528os\u6a21\u5757\u521b\u5efa\u6587\u4ef6\u5939\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def create_folder(path):<\/p>\n<p>    try:<\/p>\n<p>        os.makedirs(path, exist_ok=True)<\/p>\n<p>        print(f&quot;Folder &#39;{path}&#39; created successfully.&quot;)<\/p>\n<p>    except OSError as error:<\/p>\n<p>        print(f&quot;Error creating folder &#39;{path}&#39;: {error}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>os.makedirs<\/code>\u51fd\u6570\u7528\u4e8e\u9012\u5f52\u521b\u5efa\u76ee\u5f55\u3002<code>exist_ok=True<\/code>\u53c2\u6570\u8868\u793a\u5982\u679c\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u4e0d\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165os\u6a21\u5757<\/strong>\uff1a<\/p>\n<p> \u9996\u5148\u9700\u8981\u5bfc\u5165os\u6a21\u5757\u4ee5\u4fbf\u4f7f\u7528\u5176\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4e49\u521b\u5efa\u6587\u4ef6\u5939\u7684\u51fd\u6570<\/strong>\uff1a<\/p>\n<p> \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<code>create_folder<\/code>\uff0c\u63a5\u6536\u4e00\u4e2a\u8def\u5f84\u53c2\u6570<code>path<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528os.makedirs\u521b\u5efa\u6587\u4ef6\u5939<\/strong>\uff1a<\/p>\n<p> <code>os.makedirs<\/code>\u51fd\u6570\u7528\u4e8e\u9012\u5f52\u521b\u5efa\u76ee\u5f55\u3002\u5982\u679c\u6307\u5b9a\u7684\u8def\u5f84\u4e0d\u5b58\u5728\uff0c\u5b83\u4f1a\u521b\u5efa\u6240\u6709\u5fc5\u8981\u7684\u7236\u76ee\u5f55\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5f02\u5e38<\/strong>\uff1a<\/p>\n<p> \u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u7684OSError\u5f02\u5e38\uff0c\u4f8b\u5982\u6743\u9650\u95ee\u9898\u6216\u8def\u5f84\u65e0\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757\u521b\u5efa\u6587\u4ef6\u5939<\/p>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\uff0c\u7528\u4e8e\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u3002\u76f8\u6bd4\u4e8eos\u6a21\u5757\uff0cpathlib\u6a21\u5757\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def create_folder(path):<\/p>\n<p>    try:<\/p>\n<p>        Path(path).mkdir(parents=True, exist_ok=True)<\/p>\n<p>        print(f&quot;Folder &#39;{path}&#39; created successfully.&quot;)<\/p>\n<p>    except Exception as error:<\/p>\n<p>        print(f&quot;Error creating folder &#39;{path}&#39;: {error}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u4e86<code>Path<\/code>\u7c7b\u6765\u521b\u5efa\u6587\u4ef6\u5939\u3002<code>mkdir<\/code>\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u76ee\u5f55\uff0c\u5e76\u4e14<code>parents=True<\/code>\u53c2\u6570\u8868\u793a\u521b\u5efa\u6240\u6709\u5fc5\u8981\u7684\u7236\u76ee\u5f55\uff0c<code>exist_ok=True<\/code>\u53c2\u6570\u8868\u793a\u5982\u679c\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u4e0d\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165pathlib\u6a21\u5757<\/strong>\uff1a<\/p>\n<p> \u9996\u5148\u9700\u8981\u5bfc\u5165pathlib\u6a21\u5757\u4ee5\u4fbf\u4f7f\u7528\u5176\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4e49\u521b\u5efa\u6587\u4ef6\u5939\u7684\u51fd\u6570<\/strong>\uff1a<\/p>\n<p> \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<code>create_folder<\/code>\uff0c\u63a5\u6536\u4e00\u4e2a\u8def\u5f84\u53c2\u6570<code>path<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Path\u7c7b\u521b\u5efa\u6587\u4ef6\u5939<\/strong>\uff1a<\/p>\n<p> <code>Path(path).mkdir<\/code>\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u76ee\u5f55\u3002\u5982\u679c\u6307\u5b9a\u7684\u8def\u5f84\u4e0d\u5b58\u5728\uff0c\u5b83\u4f1a\u521b\u5efa\u6240\u6709\u5fc5\u8981\u7684\u7236\u76ee\u5f55\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5f02\u5e38<\/strong>\uff1a<\/p>\n<p> \u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001os\u6a21\u5757\u4e0epathlib\u6a21\u5757\u7684\u5bf9\u6bd4<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6613\u7528\u6027<\/strong>\uff1a<\/p>\n<p> <code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\uff0c\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u7528\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c<code>os<\/code>\u6a21\u5757\u7684\u51fd\u6570\u5f0f\u64cd\u4f5c\u7a0d\u663e\u7e41\u7410\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u529f\u80fd\u6027<\/strong>\uff1a<\/p>\n<p> <code>os<\/code>\u6a21\u5757\u529f\u80fd\u4e30\u5bcc\uff0c\u6db5\u76d6\u4e86\u66f4\u591a\u7684\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u529f\u80fd\u3002<code>pathlib<\/code>\u6a21\u5757\u4e13\u6ce8\u4e8e\u8def\u5f84\u64cd\u4f5c\uff0c\u529f\u80fd\u76f8\u5bf9\u5355\u4e00\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u517c\u5bb9\u6027<\/strong>\uff1a<\/p>\n<p> <code>pathlib<\/code>\u6a21\u5757\u662f\u5728Python 3.4\u5f15\u5165\u7684\uff0c\u4ec5\u9002\u7528\u4e8ePython 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<code>os<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u9002\u7528\u4e8e\u6240\u6709Python\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u521b\u5efa\u591a\u7ea7\u76ee\u5f55<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u8fd8\u662f<code>pathlib<\/code>\u6a21\u5757\uff0c\u90fd\u53ef\u4ee5\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\u3002\u53ea\u9700\u8981\u6307\u5b9a\u5b8c\u6574\u7684\u8def\u5f84\uff0c\u51fd\u6570\u4f1a\u81ea\u52a8\u521b\u5efa\u6240\u6709\u5fc5\u8981\u7684\u7236\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528os\u6a21\u5757\u521b\u5efa\u591a\u7ea7\u76ee\u5f55<\/p>\n<p>import os<\/p>\n<p>def create_multilevel_folder(path):<\/p>\n<p>    try:<\/p>\n<p>        os.makedirs(path, exist_ok=True)<\/p>\n<p>        print(f&quot;Multilevel folder &#39;{path}&#39; created successfully.&quot;)<\/p>\n<p>    except OSError as error:<\/p>\n<p>        print(f&quot;Error creating multilevel folder &#39;{path}&#39;: {error}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528pathlib\u6a21\u5757\u521b\u5efa\u591a\u7ea7\u76ee\u5f55<\/strong><\/h2>\n<p>from pathlib import Path<\/p>\n<p>def create_multilevel_folder(path):<\/p>\n<p>    try:<\/p>\n<p>        Path(path).mkdir(parents=True, exist_ok=True)<\/p>\n<p>        print(f&quot;Multilevel folder &#39;{path}&#39; created successfully.&quot;)<\/p>\n<p>    except Exception as error:<\/p>\n<p>        print(f&quot;Error creating multilevel folder &#39;{path}&#39;: {error}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u8def\u5f84\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8def\u5f84\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u7a7a\u683c\u3001\u53cd\u659c\u6760\u7b49\u3002\u4e3a\u4e86\u786e\u4fdd\u8def\u5f84\u80fd\u591f\u6b63\u786e\u89e3\u6790\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.join<\/code>\u51fd\u6570\u6765\u62fc\u63a5\u8def\u5f84\uff0c\u6216\u8005\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528os\u6a21\u5757\u5904\u7406\u7279\u6b8a\u5b57\u7b26<\/p>\n<p>import os<\/p>\n<p>def create_folder_with_special_chars(base_path, folder_name):<\/p>\n<p>    try:<\/p>\n<p>        full_path = os.path.join(base_path, folder_name)<\/p>\n<p>        os.makedirs(full_path, exist_ok=True)<\/p>\n<p>        print(f&quot;Folder &#39;{full_path}&#39; created successfully.&quot;)<\/p>\n<p>    except OSError as error:<\/p>\n<p>        print(f&quot;Error creating folder &#39;{full_path}&#39;: {error}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528pathlib\u6a21\u5757\u5904\u7406\u7279\u6b8a\u5b57\u7b26<\/strong><\/h2>\n<p>from pathlib import Path<\/p>\n<p>def create_folder_with_special_chars(base_path, folder_name):<\/p>\n<p>    try:<\/p>\n<p>        full_path = Path(base_path) \/ folder_name<\/p>\n<p>        full_path.mkdir(parents=True, exist_ok=True)<\/p>\n<p>        print(f&quot;Folder &#39;{full_path}&#39; created successfully.&quot;)<\/p>\n<p>    except Exception as error:<\/p>\n<p>        print(f&quot;Error creating folder &#39;{full_path}&#39;: {error}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u4e4b\u524d\uff0c\u53ef\u4ee5\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u521b\u5efa\u6587\u4ef6\u5939\u6216\u8986\u76d6\u5df2\u6709\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528os\u6a21\u5757\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/p>\n<p>import os<\/p>\n<p>def check_and_create_folder(path):<\/p>\n<p>    if not os.path.exists(path):<\/p>\n<p>        try:<\/p>\n<p>            os.makedirs(path)<\/p>\n<p>            print(f&quot;Folder &#39;{path}&#39; created successfully.&quot;)<\/p>\n<p>        except OSError as error:<\/p>\n<p>            print(f&quot;Error creating folder &#39;{path}&#39;: {error}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Folder &#39;{path}&#39; already exists.&quot;)<\/p>\n<h2><strong>\u4f7f\u7528pathlib\u6a21\u5757\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>from pathlib import Path<\/p>\n<p>def check_and_create_folder(path):<\/p>\n<p>    folder_path = Path(path)<\/p>\n<p>    if not folder_path.exists():<\/p>\n<p>        try:<\/p>\n<p>            folder_path.mkdir(parents=True, exist_ok=True)<\/p>\n<p>            print(f&quot;Folder &#39;{path}&#39; created successfully.&quot;)<\/p>\n<p>        except Exception as error:<\/p>\n<p>            print(f&quot;Error creating folder &#39;{path}&#39;: {error}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Folder &#39;{path}&#39; already exists.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p><strong>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u521b\u5efa\u6587\u4ef6\u5939\uff0c\u6700\u5e38\u7528\u4e14\u63a8\u8350\u7684\u662f\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u3002<\/strong>os\u6a21\u5757\u529f\u80fd\u4e30\u5bcc\uff0c\u9002\u7528\u4e8e\u6240\u6709Python\u7248\u672c\uff1bpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\uff0c\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u7528\u3002\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6a21\u5757\u3002\u540c\u65f6\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u591a\u7ea7\u76ee\u5f55\u3001\u7279\u6b8a\u5b57\u7b26\u8def\u5f84\uff0c\u4ee5\u53ca\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u521b\u5efa\u548c\u7ba1\u7406\u6587\u4ef6\u5939\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u5728Python\u4e2d\u521b\u5efa\u65b0\u6587\u4ef6\u5939\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>mkdir()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u5355\u4e2a\u6587\u4ef6\u5939\uff0c\u6216\u8005\u4f7f\u7528<code>makedirs()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u591a\u5c42\u6587\u4ef6\u5939\u3002\u4f8b\u5982\uff0c<code>os.mkdir(&#39;\u65b0\u6587\u4ef6\u5939&#39;)<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201c\u65b0\u6587\u4ef6\u5939\u201d\u7684\u6587\u4ef6\u5939\uff0c\u800c<code>os.makedirs(&#39;\u7236\u6587\u4ef6\u5939\/\u5b50\u6587\u4ef6\u5939&#39;)<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u201c\u7236\u6587\u4ef6\u5939\u201d\u5e76\u5728\u5176\u4e2d\u521b\u5efa\u201c\u5b50\u6587\u4ef6\u5939\u201d\u3002\u786e\u4fdd\u5728\u521b\u5efa\u6587\u4ef6\u5939\u4e4b\u524d\u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\u8bbe\u7f6e\u6743\u9650\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528<code>os.mkdir()<\/code>\u548c<code>os.makedirs()<\/code>\u65f6\u53ef\u4ee5\u8bbe\u7f6e\u6743\u9650\u3002\u901a\u8fc7\u5728\u51fd\u6570\u4e2d\u4f20\u9012\u4e00\u4e2a\u53ef\u9009\u7684<code>mode<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u5b9a\u4e49\u6587\u4ef6\u5939\u7684\u8bbf\u95ee\u6743\u9650\u3002\u4f8b\u5982\uff0c<code>os.mkdir(&#39;\u65b0\u6587\u4ef6\u5939&#39;, mode=0o755)<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u5939\uff0c\u5e76\u4e3a\u7528\u6237\u3001\u7ec4\u548c\u5176\u4ed6\u4eba\u8bbe\u7f6e\u4e0d\u540c\u7684\u8bbf\u95ee\u6743\u9650\u3002\u8bf7\u6ce8\u610f\uff0c\u6743\u9650\u8bbe\u7f6e\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u8981\u521b\u5efa\u591a\u4e2a\u6587\u4ef6\u5939\uff0c\u6709\u4ec0\u4e48\u7b80\u4fbf\u7684\u65b9\u6cd5\u5417\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u521b\u5efa\u591a\u4e2a\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u5408<code>os.mkdir()<\/code>\u6216<code>os.makedirs()<\/code>\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6587\u4ef6\u5939\u540d\u79f0\u7684\u5217\u8868\uff0c\u7136\u540e\u904d\u5386\u8fd9\u4e2a\u5217\u8868\u6765\u521b\u5efa\u6240\u6709\u6587\u4ef6\u5939\u3002\u8fd9\u6837\u53ef\u4ee5\u8282\u7701\u65f6\u95f4\u5e76\u63d0\u9ad8\u6548\u7387\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>for folder in [&#39;\u6587\u4ef6\u59391&#39;, &#39;\u6587\u4ef6\u59392&#39;, &#39;\u6587\u4ef6\u59393&#39;]: os.makedirs(folder)<\/code>\u5c06\u4e00\u6b21\u6027\u521b\u5efa\u4e09\u4e2a\u6587\u4ef6\u5939\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u521b\u5efa\u6587\u4ef6\u5939\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001shutil\u6a21\u5757\u3002\u6700\u5e38\u7528\u4e14\u63a8\u8350\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1063742,"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\/1063733"}],"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=1063733"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063733\/revisions"}],"predecessor-version":[{"id":1063745,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063733\/revisions\/1063745"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1063742"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1063733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1063733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1063733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}