{"id":1116972,"date":"2025-01-08T18:21:40","date_gmt":"2025-01-08T10:21:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1116972.html"},"modified":"2025-01-08T18:21:43","modified_gmt":"2025-01-08T10:21:43","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e5%a4%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1116972.html","title":{"rendered":"python\u5982\u4f55\u751f\u6210\u4e00\u4e2a\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\/25081059\/f7d18a71-cf0b-41a9-9413-4ee0f9230c6c.webp\" alt=\"python\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u6587\u4ef6\u5939<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3001\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u751f\u6210\u4e00\u4e2a\u6587\u4ef6\u5939\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u3002<strong>\u4f7f\u7528os\u6a21\u5757\u548c\u4f7f\u7528pathlib\u6a21\u5757\u662f\u751f\u6210\u6587\u4ef6\u5939\u7684\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c<code>os<\/code>\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u51e0\u4e4e\u6240\u6709\u7684Python\u73af\u5883\u90fd\u652f\u6301\uff0c\u800c<code>pathlib<\/code>\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\uff0c\u66f4\u52a0\u73b0\u4ee3\u548c\u6613\u4e8e\u4f7f\u7528\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e24\u79cd\u65b9\u6cd5\uff0c\u5e76\u8ba8\u8bba\u5982\u4f55\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u7528\u4e8e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u521b\u5efa\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528os.mkdir()<\/h4>\n<\/p>\n<p><p><code>os.mkdir()<\/code>\u51fd\u6570\u7528\u4e8e\u5728\u6307\u5b9a\u8def\u5f84\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &quot;new_folder&quot;<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.mkdir(folder_path)<\/p>\n<p>print(f&quot;\u6587\u4ef6\u5939 {folder_path} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u6307\u5b9a\u4e86\u4e00\u4e2a\u6587\u4ef6\u5939\u8def\u5f84<code>folder_path<\/code>\uff0c\u63a5\u7740\u4f7f\u7528<code>os.mkdir()<\/code>\u51fd\u6570\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>new_folder<\/code>\u7684\u6587\u4ef6\u5939\uff0c\u6700\u540e\u6253\u5370\u4e00\u6761\u6210\u529f\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528os.makedirs()<\/h4>\n<\/p>\n<p><p><code>os.makedirs()<\/code>\u51fd\u6570\u7528\u4e8e\u9012\u5f52\u5730\u521b\u5efa\u76ee\u5f55\u6811\uff0c\u5982\u679c\u4e2d\u95f4\u7684\u67d0\u4e9b\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5b83\u4eec\u5c06\u88ab\u4e00\u5e76\u521b\u5efa\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u591a\u7ea7\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &quot;parent_folder\/child_folder&quot;<\/p>\n<h2><strong>\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.makedirs(folder_path)<\/p>\n<p>print(f&quot;\u591a\u7ea7\u6587\u4ef6\u5939 {folder_path} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u4e00\u4e2a\u591a\u7ea7\u6587\u4ef6\u5939\u8def\u5f84<code>folder_path<\/code>\uff0c\u5e76\u4f7f\u7528<code>os.makedirs()<\/code>\u51fd\u6570\u9012\u5f52\u5730\u521b\u5efa\u4e86<code>parent_folder<\/code>\u53ca\u5176\u5b50\u76ee\u5f55<code>child_folder<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u5728Python 3.4\u4e2d\u5f15\u5165\uff0c\u662f\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u6a21\u5757\uff0c\u6bd4<code>os<\/code>\u6a21\u5757\u66f4\u4e3a\u73b0\u4ee3\u548c\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528Path.mkdir()<\/h4>\n<\/p>\n<p><p><code>pathlib.Path.mkdir()<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u76ee\u5f55\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = Path(&quot;new_folder&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>folder_path.mkdir()<\/p>\n<p>print(f&quot;\u6587\u4ef6\u5939 {folder_path} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4ece<code>pathlib<\/code>\u6a21\u5757\u5bfc\u5165\u4e86<code>Path<\/code>\u7c7b\uff0c\u7136\u540e\u6307\u5b9a\u4e86\u4e00\u4e2a\u6587\u4ef6\u5939\u8def\u5f84<code>folder_path<\/code>\uff0c\u63a5\u7740\u4f7f\u7528<code>folder_path.mkdir()<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u6587\u4ef6\u5939\uff0c\u6700\u540e\u6253\u5370\u4e00\u6761\u6210\u529f\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528Path.mkdir()\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p><code>pathlib.Path.mkdir()<\/code>\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\uff0c\u53ea\u9700\u8bbe\u7f6e<code>parents=True<\/code>\u53c2\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6307\u5b9a\u591a\u7ea7\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = Path(&quot;parent_folder\/child_folder&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>folder_path.mkdir(parents=True)<\/p>\n<p>print(f&quot;\u591a\u7ea7\u6587\u4ef6\u5939 {folder_path} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u4e00\u4e2a\u591a\u7ea7\u6587\u4ef6\u5939\u8def\u5f84<code>folder_path<\/code>\uff0c\u5e76\u4f7f\u7528<code>folder_path.mkdir(parents=True)<\/code>\u51fd\u6570\u9012\u5f52\u5730\u521b\u5efa\u4e86<code>parent_folder<\/code>\u53ca\u5176\u5b50\u76ee\u5f55<code>child_folder<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u8fd8\u662f<code>pathlib<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528os\u6a21\u5757\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>os.makedirs()<\/code>\u51fd\u6570\u53ef\u4ee5\u9012\u5f52\u5730\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u591a\u7ea7\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &quot;parent_folder\/child_folder\/grandchild_folder&quot;<\/p>\n<h2><strong>\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.makedirs(folder_path)<\/p>\n<p>print(f&quot;\u591a\u7ea7\u6587\u4ef6\u5939 {folder_path} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u4e00\u4e2a\u591a\u7ea7\u6587\u4ef6\u5939\u8def\u5f84<code>folder_path<\/code>\uff0c\u5e76\u4f7f\u7528<code>os.makedirs()<\/code>\u51fd\u6570\u9012\u5f52\u5730\u521b\u5efa\u4e86\u6240\u6709\u7ea7\u522b\u7684\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528pathlib\u6a21\u5757\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pathlib.Path.mkdir(parents=True)<\/code>\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u9012\u5f52\u5730\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6307\u5b9a\u591a\u7ea7\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = Path(&quot;parent_folder\/child_folder\/grandchild_folder&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>folder_path.mkdir(parents=True)<\/p>\n<p>print(f&quot;\u591a\u7ea7\u6587\u4ef6\u5939 {folder_path} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u4e00\u4e2a\u591a\u7ea7\u6587\u4ef6\u5939\u8def\u5f84<code>folder_path<\/code>\uff0c\u5e76\u4f7f\u7528<code>folder_path.mkdir(parents=True)<\/code>\u51fd\u6570\u9012\u5f52\u5730\u521b\u5efa\u4e86\u6240\u6709\u7ea7\u522b\u7684\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u6587\u4ef6\u5939\u5df2\u5b58\u5728\u7684\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\uff0c\u5982\u679c\u6587\u4ef6\u5939\u5df2\u7ecf\u5b58\u5728\uff0c\u53ef\u80fd\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6216\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u6765\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528os\u6a21\u5757\u5904\u7406\u6587\u4ef6\u5939\u5df2\u5b58\u5728\u7684\u60c5\u51b5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff0c\u6216\u8005\u6355\u83b7<code>FileExistsError<\/code>\u5f02\u5e38\u3002\u4e0b\u9762\u662f\u4e24\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b1\uff1a\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &quot;existing_folder&quot;<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if not os.path.exists(folder_path):<\/p>\n<p>    os.mkdir(folder_path)<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u5939 {folder_path} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u5939 {folder_path} \u5df2\u7ecf\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u793a\u4f8b2\uff1a\u6355\u83b7\u5f02\u5e38<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &quot;existing_folder&quot;<\/p>\n<p>try:<\/p>\n<p>    os.mkdir(folder_path)<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u5939 {folder_path} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p>except FileExistsError:<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u5939 {folder_path} \u5df2\u7ecf\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528pathlib\u6a21\u5757\u5904\u7406\u6587\u4ef6\u5939\u5df2\u5b58\u5728\u7684\u60c5\u51b5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>Path.exists()<\/code>\u65b9\u6cd5\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff0c\u6216\u8005\u8bbe\u7f6e<code>exist_ok=True<\/code>\u53c2\u6570\u6765\u5ffd\u7565\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939\u3002\u4e0b\u9762\u662f\u4e24\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b1\uff1a\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = Path(&quot;existing_folder&quot;)<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if not folder_path.exists():<\/p>\n<p>    folder_path.mkdir()<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u5939 {folder_path} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u5939 {folder_path} \u5df2\u7ecf\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u793a\u4f8b2\uff1a\u5ffd\u7565\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = Path(&quot;existing_folder&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6\u5939\u5e76\u5ffd\u7565\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>folder_path.mkdir(exist_ok=True)<\/p>\n<p>print(f&quot;\u6587\u4ef6\u5939 {folder_path} \u5df2\u7ecf\u5b58\u5728\u6216\u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u521b\u5efa\u5e26\u6709\u6743\u9650\u8bbe\u7f6e\u7684\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u5939\u7684\u6743\u9650\u8bbe\u7f6e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>os.mkdir()<\/code>\u51fd\u6570\u7684<code>mode<\/code>\u53c2\u6570\uff0c\u6216\u8005\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u7684<code>Path.mkdir()<\/code>\u51fd\u6570\u7684<code>mode<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528os\u6a21\u5757\u521b\u5efa\u5e26\u6709\u6743\u9650\u8bbe\u7f6e\u7684\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.mkdir()<\/code>\u51fd\u6570\u7684<code>mode<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u6743\u9650\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &quot;secure_folder&quot;<\/p>\n<h2><strong>\u521b\u5efa\u5e26\u6709\u6743\u9650\u8bbe\u7f6e\u7684\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.mkdir(folder_path, mode=0o755)<\/p>\n<p>print(f&quot;\u5e26\u6709\u6743\u9650\u8bbe\u7f6e\u7684\u6587\u4ef6\u5939 {folder_path} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u4e00\u4e2a\u6587\u4ef6\u5939\u8def\u5f84<code>folder_path<\/code>\uff0c\u5e76\u4f7f\u7528<code>os.mkdir()<\/code>\u51fd\u6570\u7684<code>mode<\/code>\u53c2\u6570\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u6743\u9650\u4e3a<code>0o755<\/code>\uff0c\u6700\u540e\u6253\u5370\u4e00\u6761\u6210\u529f\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528pathlib\u6a21\u5757\u521b\u5efa\u5e26\u6709\u6743\u9650\u8bbe\u7f6e\u7684\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>Path.mkdir()<\/code>\u51fd\u6570\u7684<code>mode<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u6743\u9650\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = Path(&quot;secure_folder&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5e26\u6709\u6743\u9650\u8bbe\u7f6e\u7684\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>folder_path.mkdir(mode=0o755)<\/p>\n<p>print(f&quot;\u5e26\u6709\u6743\u9650\u8bbe\u7f6e\u7684\u6587\u4ef6\u5939 {folder_path} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u4e00\u4e2a\u6587\u4ef6\u5939\u8def\u5f84<code>folder_path<\/code>\uff0c\u5e76\u4f7f\u7528<code>Path.mkdir()<\/code>\u51fd\u6570\u7684<code>mode<\/code>\u53c2\u6570\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u6743\u9650\u4e3a<code>0o755<\/code>\uff0c\u6700\u540e\u6253\u5370\u4e00\u6761\u6210\u529f\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u751f\u6210\u4e00\u4e2a\u6587\u4ef6\u5939\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u3002<strong>\u4f7f\u7528os\u6a21\u5757\u548c\u4f7f\u7528pathlib\u6a21\u5757\u662f\u751f\u6210\u6587\u4ef6\u5939\u7684\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c<code>os<\/code>\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u51e0\u4e4e\u6240\u6709\u7684Python\u73af\u5883\u90fd\u652f\u6301\uff0c\u800c<code>pathlib<\/code>\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\uff0c\u66f4\u52a0\u73b0\u4ee3\u548c\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u5230\u5982\u4f55\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u521b\u5efa\u5355\u7ea7\u548c\u591a\u7ea7\u6587\u4ef6\u5939\uff0c\u5e76\u5904\u7406\u6587\u4ef6\u5939\u5df2\u5b58\u5728\u7684\u60c5\u51b5\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\u6307\u5b9a\u6743\u9650\u8bbe\u7f6e\u3002\u5e0c\u671b\u672c\u6587\u7684\u4ecb\u7ecd\u80fd\u591f\u5e2e\u52a9\u60a8\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u4f7f\u7528Python\u751f\u6210\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u591a\u4e2a\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>makedirs()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u591a\u4e2a\u5d4c\u5957\u6587\u4ef6\u5939\u3002\u4f8b\u5982\uff0c<code>os.makedirs(&#39;parent\/child\/grandchild&#39;)<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>parent<\/code>\u7684\u6587\u4ef6\u5939\uff0c\u91cc\u9762\u5305\u542b\u4e00\u4e2a\u540d\u4e3a<code>child<\/code>\u7684\u5b50\u6587\u4ef6\u5939\uff0c<code>child<\/code>\u91cc\u9762\u53c8\u5305\u542b\u4e00\u4e2a\u540d\u4e3a<code>grandchild<\/code>\u7684\u5b50\u6587\u4ef6\u5939\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u9700\u8981\u521b\u5efa\u591a\u5c42\u7ea7\u7ed3\u6784\u7684\u60c5\u51b5\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 \/>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>makedirs()<\/code>\u6216<code>mkdir()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u6dfb\u52a0\u53c2\u6570\u4ee5\u9632\u6b62\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bbe\u7f6e<code>exist_ok=True<\/code>\uff0c\u8fd9\u6837\u5982\u679c\u6587\u4ef6\u5939\u5df2\u5b58\u5728\uff0c\u5c31\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u4ee3\u7801\u793a\u4f8b\uff1a<code>os.makedirs(&#39;folder_name&#39;, exist_ok=True)<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u521b\u5efa\u6587\u4ef6\u5939\u65f6\u4e0d\u4f1a\u5e72\u6270\u73b0\u6709\u7684\u6587\u4ef6\u5939\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u6ca1\u6709\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>os<\/code>\u6a21\u5757\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5229\u7528<code>pathlib<\/code>\u6a21\u5757\u6765\u7b80\u5316\u6587\u4ef6\u5939\u7684\u521b\u5efa\u8fc7\u7a0b\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a<code>Path<\/code>\u5bf9\u8c61\u5e76\u8c03\u7528<code>.mkdir()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u521b\u5efa\u6587\u4ef6\u5939\u3002\u4f8b\u5982\uff0c<code>from pathlib import Path; Path(&#39;new_folder&#39;).mkdir(parents=True, exist_ok=True)<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u5e76\u4e14\u652f\u6301\u94fe\u5f0f\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u6587\u4ef6\u5939 \u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3001\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939 \u5728Python\u4e2d\uff0c\u751f\u6210\u4e00 [&hellip;]","protected":false},"author":3,"featured_media":1116981,"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\/1116972"}],"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=1116972"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116972\/revisions"}],"predecessor-version":[{"id":1116984,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116972\/revisions\/1116984"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1116981"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1116972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1116972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1116972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}