{"id":1174691,"date":"2025-01-15T17:21:54","date_gmt":"2025-01-15T09:21:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174691.html"},"modified":"2025-01-15T17:21:56","modified_gmt":"2025-01-15T09:21:56","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e9%80%a0%e6%96%87%e4%bb%b6%e5%a4%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1174691.html","title":{"rendered":"python\u5982\u4f55\u521b\u9020\u6587\u4ef6\u5939"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110338\/7069fd4d-ba58-417a-9514-471a2a96c473.webp\" alt=\"python\u5982\u4f55\u521b\u9020\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u521b\u5efa\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>os\u6a21\u5757\u3001pathlib\u6a21\u5757<\/strong>\u3002\u5176\u4e2d\uff0cos\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0cpathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\u3002<strong>\u4f7f\u7528os\u6a21\u5757\u7684os.mkdir()\u51fd\u6570\u3001\u4f7f\u7528pathlib\u6a21\u5757\u7684Path.mkdir()\u65b9\u6cd5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528os\u6a21\u5757\u7684os.mkdir()\u51fd\u6570\u521b\u5efa\u6587\u4ef6\u5939\u975e\u5e38\u7b80\u5355\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u521b\u5efa\u7684\u6587\u4ef6\u5939\u7684\u8def\u5f84\u3002\u5982\u679c\u8def\u5f84\u4e2d\u5305\u542b\u591a\u4e2a\u76ee\u5f55\uff0c\u5219\u9700\u8981\u9010\u7ea7\u521b\u5efa\uff0c\u6216\u8005\u4f7f\u7528os.makedirs()\u51fd\u6570\u4e00\u6b21\u6027\u521b\u5efa\u6240\u6709\u76ee\u5f55\u3002\u800c\u4f7f\u7528pathlib\u6a21\u5757\u7684Path.mkdir()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u521b\u5efa\u6587\u4ef6\u5939\uff0c\u4e14\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570\u5b9e\u73b0\u9012\u5f52\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757\u521b\u5efa\u6587\u4ef6\u5939<\/p>\n<\/p>\n<ol>\n<li><strong>os.mkdir()<\/strong><\/li>\n<\/ol>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u4f7f\u7528\u3002os.mkdir()\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u5355\u7ea7\u76ee\u5f55\uff0c\u5373\u53ea\u521b\u5efa\u6307\u5b9a\u8def\u5f84\u4e2d\u6700\u540e\u4e00\u7ea7\u76ee\u5f55\u3002\u5982\u679c\u8def\u5f84\u4e2d\u5305\u542b\u7684\u67d0\u4e9b\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1FileNotFoundError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u5355\u7ea7\u76ee\u5f55<\/strong><\/h2>\n<p>folder_path = &#39;example_folder&#39;<\/p>\n<p>os.mkdir(folder_path)<\/p>\n<p>print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example_folder<\/code>\u7684\u6587\u4ef6\u5939\u3002\u5982\u679c\u8be5\u6587\u4ef6\u5939\u5df2\u5b58\u5728\uff0c\u5c06\u5f15\u53d1FileExistsError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>os.makedirs()<\/strong><\/li>\n<\/ol>\n<p><p>os.makedirs()\u51fd\u6570\u7528\u4e8e\u9012\u5f52\u521b\u5efa\u76ee\u5f55\uff0c\u5373\u53ef\u4ee5\u4e00\u6b21\u6027\u521b\u5efa\u6307\u5b9a\u8def\u5f84\u4e2d\u7684\u6240\u6709\u76ee\u5f55\u3002\u5982\u679c\u8def\u5f84\u4e2d\u5305\u542b\u7684\u67d0\u4e9b\u76ee\u5f55\u4e0d\u5b58\u5728\uff0cos.makedirs()\u51fd\u6570\u4f1a\u81ea\u52a8\u521b\u5efa\u8fd9\u4e9b\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u591a\u7ea7\u76ee\u5f55<\/strong><\/h2>\n<p>folder_path = &#39;example_folder\/sub_folder&#39;<\/p>\n<p>os.makedirs(folder_path)<\/p>\n<p>print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example_folder\/sub_folder<\/code>\u7684\u591a\u7ea7\u76ee\u5f55\u3002\u5982\u679c\u8fd9\u4e9b\u76ee\u5f55\u5df2\u5b58\u5728\uff0cos.makedirs()\u51fd\u6570\u4e0d\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757\u521b\u5efa\u6587\u4ef6\u5939<\/p>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u8def\u5f84\u3002\u4f7f\u7528pathlib\u6a21\u5757\u7684Path.mkdir()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u521b\u5efa\u76ee\u5f55\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>Path.mkdir()<\/strong><\/li>\n<\/ol>\n<p><p>Path.mkdir()\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u5355\u7ea7\u76ee\u5f55\u548c\u591a\u7ea7\u76ee\u5f55\u3002\u4e0eos.mkdir()\u7c7b\u4f3c\uff0c\u5982\u679c\u8def\u5f84\u4e2d\u5305\u542b\u7684\u67d0\u4e9b\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5c06\u5f15\u53d1FileNotFoundError\u5f02\u5e38\u3002\u4e0d\u8fc7\uff0c\u901a\u8fc7\u8bbe\u7f6eparents\u53c2\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9012\u5f52\u521b\u5efa\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u5355\u7ea7\u76ee\u5f55<\/strong><\/h2>\n<p>folder_path = Path(&#39;example_folder&#39;)<\/p>\n<p>folder_path.mkdir()<\/p>\n<p>print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example_folder<\/code>\u7684\u6587\u4ef6\u5939\u3002\u5982\u679c\u8be5\u6587\u4ef6\u5939\u5df2\u5b58\u5728\uff0c\u5c06\u5f15\u53d1FileExistsError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>Path.mkdir() with parents=True<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8bbe\u7f6ePath.mkdir()\u65b9\u6cd5\u7684parents\u53c2\u6570\u4e3aTrue\uff0c\u53ef\u4ee5\u9012\u5f52\u521b\u5efa\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u591a\u7ea7\u76ee\u5f55<\/strong><\/h2>\n<p>folder_path = Path(&#39;example_folder\/sub_folder&#39;)<\/p>\n<p>folder_path.mkdir(parents=True)<\/p>\n<p>print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example_folder\/sub_folder<\/code>\u7684\u591a\u7ea7\u76ee\u5f55\u3002\u5982\u679c\u8fd9\u4e9b\u76ee\u5f55\u5df2\u5b58\u5728\uff0cPath.mkdir()\u65b9\u6cd5\u4e0d\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5904\u7406\u6587\u4ef6\u5939\u521b\u5efa\u7684\u5f02\u5e38<\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u6587\u4ef6\u5939\u5df2\u5b58\u5728\u3001\u8def\u5f84\u65e0\u6548\u7b49\u3002\u4e3a\u4e86\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528try&#8230;except\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6355\u83b7FileExistsError\u5f02\u5e38<\/strong><\/li>\n<\/ol>\n<p><p>\u5f53\u8981\u521b\u5efa\u7684\u6587\u4ef6\u5939\u5df2\u5b58\u5728\u65f6\uff0c\u5c06\u5f15\u53d1FileExistsError\u5f02\u5e38\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u8be5\u5f02\u5e38\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>folder_path = &#39;example_folder&#39;<\/p>\n<p>try:<\/p>\n<p>    os.mkdir(folder_path)<\/p>\n<p>    print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<p>except FileExistsError:<\/p>\n<p>    print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u5df2\u5b58\u5728&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example_folder<\/code>\u7684\u6587\u4ef6\u5939\u3002\u5982\u679c\u8be5\u6587\u4ef6\u5939\u5df2\u5b58\u5728\uff0c\u5c06\u6355\u83b7FileExistsError\u5f02\u5e38\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6355\u83b7\u5176\u4ed6\u5f02\u5e38<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u8fd8\u4f1a\u9047\u5230\u5176\u4ed6\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u8def\u5f84\u65e0\u6548\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u901a\u7528\u7684Exception\u5f02\u5e38\u6765\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>folder_path = &#39;example_folder&#39;<\/p>\n<p>try:<\/p>\n<p>    os.mkdir(folder_path)<\/p>\n<p>    print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<p>except FileExistsError:<\/p>\n<p>    print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u5df2\u5b58\u5728&#39;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&#39;\u521b\u5efa\u6587\u4ef6\u5939\u65f6\u53d1\u751f\u9519\u8bef: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example_folder<\/code>\u7684\u6587\u4ef6\u5939\u3002\u5982\u679c\u53d1\u751f\u5176\u4ed6\u5f02\u5e38\uff0c\u5c06\u6355\u83b7Exception\u5f02\u5e38\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u521b\u5efa\u6587\u4ef6\u5939\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff0c\u907f\u514d\u5f15\u53d1FileExistsError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>folder_path = &#39;example_folder&#39;<\/p>\n<p>if not os.path.exists(folder_path):<\/p>\n<p>    os.mkdir(folder_path)<\/p>\n<p>    print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u5df2\u5b58\u5728&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5148\u68c0\u67e5\u6587\u4ef6\u5939<code>example_folder<\/code>\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u8be5\u6587\u4ef6\u5939\uff1b\u5982\u679c\u5df2\u5b58\u5728\uff0c\u5219\u8f93\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528pathlib\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u8def\u5f84\uff0c\u5efa\u8bae\u4f18\u5148\u4f7f\u7528pathlib\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>folder_path = Path(&#39;example_folder&#39;)<\/p>\n<p>if not folder_path.exists():<\/p>\n<p>    folder_path.mkdir()<\/p>\n<p>    print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u5df2\u5b58\u5728&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5148\u68c0\u67e5\u6587\u4ef6\u5939<code>example_folder<\/code>\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u8be5\u6587\u4ef6\u5939\uff1b\u5982\u679c\u5df2\u5b58\u5728\uff0c\u5219\u8f93\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5904\u7406\u5f02\u5e38<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u4e3a\u4e86\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528try&#8230;except\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>folder_path = Path(&#39;example_folder&#39;)<\/p>\n<p>try:<\/p>\n<p>    if not folder_path.exists():<\/p>\n<p>        folder_path.mkdir()<\/p>\n<p>        print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u5df2\u5b58\u5728&#39;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&#39;\u521b\u5efa\u6587\u4ef6\u5939\u65f6\u53d1\u751f\u9519\u8bef: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example_folder<\/code>\u7684\u6587\u4ef6\u5939\u3002\u5982\u679c\u53d1\u751f\u5f02\u5e38\uff0c\u5c06\u6355\u83b7Exception\u5f02\u5e38\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528os\u6a21\u5757\u4e0epathlib\u6a21\u5757\u7684\u5bf9\u6bd4<\/p>\n<\/p>\n<p><p>\u867d\u7136os\u6a21\u5757\u548cpathlib\u6a21\u5757\u90fd\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u6587\u4ef6\u5939\uff0c\u4f46\u5b83\u4eec\u5728\u4f7f\u7528\u65b9\u5f0f\u548c\u529f\u80fd\u4e0a\u6709\u6240\u4e0d\u540c\u3002\u4e0b\u9762\u5c06\u5bf9\u6bd4\u8fd9\u4e24\u4e2a\u6a21\u5757\u7684\u4f18\u7f3a\u70b9\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u9009\u62e9\u9002\u5408\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>os\u6a21\u5757\u7684\u4f18\u7f3a\u70b9<\/strong><\/li>\n<\/ol>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1aos\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6807\u51c6\u5e93\uff0c\u76f4\u63a5\u5bfc\u5165\u5373\u53ef\u4f7f\u7528\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027\u597d<\/strong>\uff1aos\u6a21\u5757\u9002\u7528\u4e8ePython\u7684\u6240\u6709\u7248\u672c\uff0c\u5177\u6709\u8f83\u597d\u7684\u517c\u5bb9\u6027\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4e0d\u591f\u76f4\u89c2<\/strong>\uff1aos\u6a21\u5757\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\u4e0d\u591f\u76f4\u89c2\uff0c\u4e0d\u5982pathlib\u6a21\u5757\u7684\u9762\u5411\u5bf9\u8c61\u98ce\u683c\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<li><strong>\u529f\u80fd\u76f8\u5bf9\u5355\u4e00<\/strong>\uff1aos\u6a21\u5757\u4e3b\u8981\u63d0\u4f9b\u57fa\u672c\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u529f\u80fd\uff0c\u529f\u80fd\u76f8\u5bf9\u5355\u4e00\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>pathlib\u6a21\u5757\u7684\u4f18\u7f3a\u70b9<\/strong><\/li>\n<\/ol>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u9762\u5411\u5bf9\u8c61<\/strong>\uff1apathlib\u6a21\u5757\u91c7\u7528\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u98ce\u683c\uff0c\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<li><strong>\u529f\u80fd\u4e30\u5bcc<\/strong>\uff1apathlib\u6a21\u5757\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u529f\u80fd\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f8b\u5982\u8def\u5f84\u89e3\u6790\u3001\u6587\u4ef6\u8bfb\u53d6\u7b49\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Python\u7248\u672c\u8981\u6c42\u8f83\u9ad8<\/strong>\uff1apathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u6807\u51c6\u5e93\uff0c\u65e9\u671f\u7248\u672c\u7684Python\u4e0d\u652f\u6301\u8be5\u6a21\u5757\u3002<\/li>\n<li><strong>\u5b66\u4e60\u6210\u672c\u8f83\u9ad8<\/strong>\uff1a\u5bf9\u4e8e\u4e60\u60ef\u4e86\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0cpathlib\u6a21\u5757\u7684\u9762\u5411\u5bf9\u8c61\u98ce\u683c\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\u6765\u9002\u5e94\u3002<\/li>\n<\/ul>\n<p><p>\u516d\u3001\u521b\u5efa\u6587\u4ef6\u5939\u7684\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u521b\u5efa\u6587\u4ef6\u5939\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u4e0b\u9762\u5c06\u4ecb\u7ecd\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u81ea\u52a8\u5316\u811a\u672c<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u81ea\u52a8\u5316\u811a\u672c\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\u5939\u6765\u5b58\u50a8\u4e2d\u95f4\u7ed3\u679c\u6216\u65e5\u5fd7\u6587\u4ef6\u3002\u901a\u8fc7\u5728\u811a\u672c\u4e2d\u521b\u5efa\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u786e\u4fdd\u6bcf\u6b21\u8fd0\u884c\u811a\u672c\u65f6\u90fd\u6709\u4e00\u4e2a\u5e72\u51c0\u7684\u5de5\u4f5c\u73af\u5883\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>temp_folder = &#39;temp&#39;<\/p>\n<p>if not os.path.exists(temp_folder):<\/p>\n<p>    os.mkdir(temp_folder)<\/p>\n<p>    print(f&#39;\u4e34\u65f6\u6587\u4ef6\u5939&quot;{temp_folder}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<h2><strong>\u8fd0\u884c\u81ea\u52a8\u5316\u4efb\u52a1<\/strong><\/h2>\n<h2><strong>...<\/strong><\/h2>\n<h2><strong>\u5220\u9664\u4e34\u65f6\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.rmdir(temp_folder)<\/p>\n<p>print(f&#39;\u4e34\u65f6\u6587\u4ef6\u5939&quot;{temp_folder}&quot;\u5df2\u5220\u9664&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>temp<\/code>\u7684\u4e34\u65f6\u6587\u4ef6\u5939\uff0c\u8fd0\u884c\u81ea\u52a8\u5316\u4efb\u52a1\u540e\uff0c\u518d\u5220\u9664\u8be5\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6570\u636e\u5b58\u50a8<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u521b\u5efa\u6587\u4ef6\u5939\u6765\u5b58\u50a8\u5904\u7406\u540e\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u65e5\u671f\u521b\u5efa\u6587\u4ef6\u5939\uff0c\u5c06\u6bcf\u5929\u7684\u6570\u636e\u5b58\u50a8\u5728\u76f8\u5e94\u7684\u6587\u4ef6\u5939\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f<\/strong><\/h2>\n<p>today = datetime.date.today()<\/p>\n<p>folder_path = Path(f&#39;data\/{today}&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>if not folder_path.exists():<\/p>\n<p>    folder_path.mkdir(parents=True)<\/p>\n<p>    print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<h2><strong>\u5b58\u50a8\u6570\u636e<\/strong><\/h2>\n<h2><strong>...<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u6839\u636e\u5f53\u524d\u65e5\u671f\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u5e76\u5c06\u6570\u636e\u5b58\u50a8\u5728\u8be5\u6587\u4ef6\u5939\u4e2d\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u9879\u76ee\u7ed3\u6784\u7ba1\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5f00\u53d1\u9879\u76ee\u65f6\uff0c\u901a\u5e38\u9700\u8981\u6309\u7167\u4e00\u5b9a\u7684\u7ed3\u6784\u7ec4\u7ec7\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u591a\u4e2a\u6587\u4ef6\u5939\u6765\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u9879\u76ee\u76ee\u5f55\u7ed3\u6784<\/strong><\/h2>\n<p>project_structure = [<\/p>\n<p>    &#39;src&#39;,<\/p>\n<p>    &#39;src\/models&#39;,<\/p>\n<p>    &#39;src\/controllers&#39;,<\/p>\n<p>    &#39;src\/views&#39;,<\/p>\n<p>    &#39;tests&#39;,<\/p>\n<p>    &#39;docs&#39;<\/p>\n<p>]<\/p>\n<h2><strong>\u521b\u5efa\u9879\u76ee\u76ee\u5f55<\/strong><\/h2>\n<p>for folder in project_structure:<\/p>\n<p>    folder_path = Path(folder)<\/p>\n<p>    if not folder_path.exists():<\/p>\n<p>        folder_path.mkdir(parents=True)<\/p>\n<p>        print(f&#39;\u6587\u4ef6\u5939&quot;{folder_path}&quot;\u521b\u5efa\u6210\u529f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u6839\u636e\u9884\u5b9a\u4e49\u7684\u9879\u76ee\u7ed3\u6784\uff0c\u521b\u5efa\u76f8\u5e94\u7684\u6587\u4ef6\u5939\uff0c\u786e\u4fdd\u9879\u76ee\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u7ec4\u7ec7\u7ed3\u6784\u6e05\u6670\u5408\u7406\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u5728Python\u4e2d\u521b\u5efa\u6587\u4ef6\u5939\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u57fa\u672c\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e24\u4e2a\u6a21\u5757\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728Python\u4e2d\u521b\u5efa\u6587\u4ef6\u5939\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u4f7f\u7528Python\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939\u53ef\u4ee5\u901a\u8fc7<code>os.makedirs()<\/code>\u51fd\u6570\u5b9e\u73b0\u3002\u8be5\u51fd\u6570\u80fd\u591f\u9012\u5f52\u5730\u521b\u5efa\u6240\u6709\u5fc5\u8981\u7684\u4e2d\u95f4\u76ee\u5f55\u3002\u4f8b\u5982\uff0c<code>os.makedirs(&#39;parent\/child\/grandchild&#39;)<\/code>\u5c06\u4f1a\u521b\u5efa<code>parent<\/code>\u3001<code>child<\/code>\u548c<code>grandchild<\/code>\u4e09\u4e2a\u6587\u4ef6\u5939\uff0c\u5982\u679c\u5b83\u4eec\u4e0d\u5b58\u5728\u7684\u8bdd\u3002\u786e\u4fdd\u5728\u4f7f\u7528\u524d\u5bfc\u5165<code>os<\/code>\u6a21\u5757\u3002<\/p>\n<p><strong>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\u5982\u4f55\u5904\u7406\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u5982\u679c\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u6587\u4ef6\u5939\uff0cPython\u5c06\u4f1a\u629b\u51fa<code>FileExistsError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\u8bbe\u7f6e<code>exist_ok=True<\/code>\u53c2\u6570\uff0c\u4f8b\u5982<code>os.makedirs(&#39;folder_name&#39;, exist_ok=True)<\/code>\uff0c\u8fd9\u6837\u5373\u4f7f\u6587\u4ef6\u5939\u5df2\u5b58\u5728\uff0c\u7a0b\u5e8f\u4e5f\u4e0d\u4f1a\u62a5\u9519\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u521b\u5efa\u7684\u6587\u4ef6\u5939\u5177\u6709\u7279\u5b9a\u6743\u9650\uff1f<\/strong><br \/>\u5728Python\u4e2d\u521b\u5efa\u6587\u4ef6\u5939\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.mkdir()<\/code>\u6216<code>os.makedirs()<\/code>\u7684<code>mode<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u6743\u9650\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>os.makedirs(&#39;folder_name&#39;, mode=0o755)<\/code>\u53ef\u4ee5\u4e3a\u65b0\u521b\u5efa\u7684\u6587\u4ef6\u5939\u8bbe\u7f6e755\u6743\u9650\u3002\u8fd9\u610f\u5473\u7740\u6240\u6709\u8005\u53ef\u4ee5\u8bfb\u3001\u5199\u548c\u6267\u884c\uff0c\u7ec4\u7528\u6237\u548c\u5176\u4ed6\u7528\u6237\u53ef\u4ee5\u8bfb\u548c\u6267\u884c\u3002\u786e\u4fdd\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u9002\u5f53\u7684\u6743\u9650\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u521b\u5efa\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3002\u5176\u4e2d\uff0cos\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6807\u51c6\u5e93\u4e4b [&hellip;]","protected":false},"author":3,"featured_media":1174696,"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\/1174691"}],"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=1174691"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174691\/revisions"}],"predecessor-version":[{"id":1174698,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174691\/revisions\/1174698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1174696"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1174691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1174691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1174691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}