{"id":1127044,"date":"2025-01-08T20:06:45","date_gmt":"2025-01-08T12:06:45","guid":{"rendered":""},"modified":"2025-01-08T20:06:47","modified_gmt":"2025-01-08T12:06:47","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%96%b0%e5%bb%ba%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e5%a4%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1127044.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u65b0\u5efa\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\/25094139\/ce8f1260-4cbc-4b71-a989-1f0f00ee4dca.webp\" alt=\"python\u4e2d\u5982\u4f55\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>mkdir()<\/code>\u6216<code>makedirs()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3001\u8fd9\u4e24\u79cd\u65b9\u6cd5\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u5b83\u4eec\u5904\u7406\u8def\u5f84\u7684\u65b9\u5f0f\u4e0d\u540c\u3001\u4f7f\u7528<code>mkdir()<\/code>\u53ef\u4ee5\u65b0\u5efa\u5355\u5c42\u6587\u4ef6\u5939\uff0c\u800c<code>makedirs()<\/code>\u5219\u53ef\u4ee5\u65b0\u5efa\u591a\u5c42\u6587\u4ef6\u5939\u3002<\/strong>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e24\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u9645\u7684\u793a\u4f8b\u4ee3\u7801\u6765\u8bf4\u660e\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u65b0\u5efa\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>os.mkdir()<\/code>\u65b0\u5efa\u5355\u5c42\u6587\u4ef6\u5939<\/p>\n<\/p>\n<p><p><code>os.mkdir()<\/code>\u51fd\u6570\u7528\u4e8e\u5728\u6307\u5b9a\u8def\u5f84\u4e0b\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\u3002\u5982\u679c\u8def\u5f84\u4e2d\u5305\u542b\u7684\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c<code>mkdir()<\/code>\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2a<code>FileNotFoundError<\/code>\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528<code>os.mkdir()<\/code>\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u786e\u4fdd\u8def\u5f84\u7684\u4e0a\u5c42\u76ee\u5f55\u662f\u5b58\u5728\u7684\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5b9a\u4e49\u8981\u65b0\u5efa\u7684\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &quot;example_folder&quot;<\/p>\n<h2><strong>\u4f7f\u7528os.mkdir()\u65b0\u5efa\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>try:<\/p>\n<p>    os.mkdir(folder_path)<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u5939 &#39;{folder_path}&#39; \u65b0\u5efa\u6210\u529f&quot;)<\/p>\n<p>except FileExistsError:<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u5939 &#39;{folder_path}&#39; \u5df2\u7ecf\u5b58\u5728&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u65b0\u5efa\u6587\u4ef6\u5939\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u8981\u65b0\u5efa\u7684\u6587\u4ef6\u5939\u8def\u5f84<code>folder_path<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528<code>os.mkdir()<\/code>\u51fd\u6570\u5c1d\u8bd5\u65b0\u5efa\u6587\u4ef6\u5939\uff0c\u5e76\u6355\u6349\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>os.makedirs()<\/code>\u65b0\u5efa\u591a\u5c42\u6587\u4ef6\u5939<\/p>\n<\/p>\n<p><p><code>os.makedirs()<\/code>\u51fd\u6570\u4e0e<code>os.mkdir()<\/code>\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u5b83\u53ef\u4ee5\u9012\u5f52\u5730\u521b\u5efa\u591a\u5c42\u76ee\u5f55\u3002\u5982\u679c\u8def\u5f84\u4e2d\u7684\u67d0\u4e9b\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c<code>os.makedirs()<\/code>\u5c06\u4f1a\u81ea\u52a8\u521b\u5efa\u8fd9\u4e9b\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5b9a\u4e49\u8981\u65b0\u5efa\u7684\u591a\u5c42\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>multi_level_folder_path = &quot;example_folder\/sub_folder&quot;<\/p>\n<h2><strong>\u4f7f\u7528os.makedirs()\u65b0\u5efa\u591a\u5c42\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>try:<\/p>\n<p>    os.makedirs(multi_level_folder_path)<\/p>\n<p>    print(f&quot;\u591a\u5c42\u6587\u4ef6\u5939 &#39;{multi_level_folder_path}&#39; \u65b0\u5efa\u6210\u529f&quot;)<\/p>\n<p>except FileExistsError:<\/p>\n<p>    print(f&quot;\u591a\u5c42\u6587\u4ef6\u5939 &#39;{multi_level_folder_path}&#39; \u5df2\u7ecf\u5b58\u5728&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u65b0\u5efa\u591a\u5c42\u6587\u4ef6\u5939\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u591a\u5c42\u6587\u4ef6\u5939\u8def\u5f84<code>multi_level_folder_path<\/code>\uff0c\u5e76\u4f7f\u7528<code>os.makedirs()<\/code>\u51fd\u6570\u6765\u65b0\u5efa\u8fd9\u4e9b\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5904\u7406\u6587\u4ef6\u5939\u5df2\u5b58\u5728\u7684\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u5939\u5df2\u7ecf\u5b58\u5728\u7684\u60c5\u51b5\u3002\u4e3a\u4e86\u907f\u514d\u7a0b\u5e8f\u629b\u51fa\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u65b0\u5efa\u6587\u4ef6\u5939\u4e4b\u524d\u68c0\u67e5\u76ee\u6807\u8def\u5f84\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def 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;\u6587\u4ef6\u5939 &#39;{path}&#39; \u65b0\u5efa\u6210\u529f&quot;)<\/p>\n<p>        except Exception as e:<\/p>\n<p>            print(f&quot;\u65b0\u5efa\u6587\u4ef6\u5939\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;\u6587\u4ef6\u5939 &#39;{path}&#39; \u5df2\u7ecf\u5b58\u5728&quot;)<\/p>\n<h2><strong>\u65b0\u5efa\u5355\u5c42\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>create_folder(&quot;example_folder&quot;)<\/p>\n<h2><strong>\u65b0\u5efa\u591a\u5c42\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>create_folder(&quot;example_folder\/sub_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>create_folder()<\/code>\uff0c\u8be5\u51fd\u6570\u5728\u65b0\u5efa\u6587\u4ef6\u5939\u4e4b\u524d\u4f1a\u68c0\u67e5\u76ee\u6807\u8def\u5f84\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u3002\u5982\u679c\u76ee\u6807\u8def\u5f84\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u4f7f\u7528<code>os.makedirs()<\/code>\u51fd\u6570\u6765\u65b0\u5efa\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>Pathlib<\/code>\u6a21\u5757\u65b0\u5efa\u6587\u4ef6\u5939<\/p>\n<\/p>\n<p><p>\u9664\u4e86<code>os<\/code>\u6a21\u5757\uff0cPython\u8fd8\u63d0\u4f9b\u4e86<code>pathlib<\/code>\u6a21\u5757\u6765\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\u3002<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u5b9a\u4e49\u8981\u65b0\u5efa\u7684\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = Path(&quot;example_folder&quot;)<\/p>\n<h2><strong>\u4f7f\u7528Path.mkdir()\u65b0\u5efa\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>try:<\/p>\n<p>    folder_path.mkdir(parents=True, exist_ok=True)<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u5939 &#39;{folder_path}&#39; \u65b0\u5efa\u6210\u529f&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u65b0\u5efa\u6587\u4ef6\u5939\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4ece<code>pathlib<\/code>\u6a21\u5757\u5bfc\u5165\u4e86<code>Path<\/code>\u7c7b\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u8981\u65b0\u5efa\u7684\u6587\u4ef6\u5939\u8def\u5f84<code>folder_path<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528<code>Path.mkdir()<\/code>\u65b9\u6cd5\u6765\u65b0\u5efa\u6587\u4ef6\u5939\u3002\u53c2\u6570<code>parents=True<\/code>\u8868\u793a\u5982\u679c\u7236\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5219\u81ea\u52a8\u521b\u5efa\u7236\u76ee\u5f55\uff0c<code>exist_ok=True<\/code>\u8868\u793a\u5982\u679c\u76ee\u6807\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7ed3\u5408\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u65b0\u5efa\u6587\u4ef6\u5939<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728\u65b0\u5efa\u6587\u4ef6\u5939\u540e\u6267\u884c\u4e00\u4e9b\u7279\u5b9a\u7684\u64cd\u4f5c\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u7ba1\u7406\u6587\u4ef6\u5939\u7684\u521b\u5efa\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from contextlib import contextmanager<\/p>\n<p>@contextmanager<\/p>\n<p>def managed_folder(path):<\/p>\n<p>    try:<\/p>\n<p>        os.makedirs(path, exist_ok=True)<\/p>\n<p>        print(f&quot;\u6587\u4ef6\u5939 &#39;{path}&#39; \u65b0\u5efa\u6210\u529f&quot;)<\/p>\n<p>        yield path<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;\u65b0\u5efa\u6587\u4ef6\u5939\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        print(f&quot;\u5b8c\u6210\u5bf9\u6587\u4ef6\u5939 &#39;{path}&#39; \u7684\u64cd\u4f5c&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u65b0\u5efa\u6587\u4ef6\u5939\u5e76\u6267\u884c\u64cd\u4f5c<\/strong><\/h2>\n<p>with managed_folder(&quot;example_folder&quot;) as folder:<\/p>\n<p>    # \u5728\u65b0\u5efa\u7684\u6587\u4ef6\u5939\u4e2d\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c<\/p>\n<p>    file_path = os.path.join(folder, &quot;example.txt&quot;)<\/p>\n<p>    with open(file_path, &quot;w&quot;) as file:<\/p>\n<p>        file.write(&quot;Hello, World!&quot;)<\/p>\n<p>    print(f&quot;\u6587\u4ef6 &#39;{file_path}&#39; \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<code>managed_folder()<\/code>\uff0c\u8be5\u7ba1\u7406\u5668\u5728\u65b0\u5efa\u6587\u4ef6\u5939\u540e\u4f1a\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\u3002\u6211\u4eec\u5728\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5185\u90e8\u65b0\u5efa\u4e86\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u5199\u5165\u4e86\u4e00\u4e9b\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u6587\u4ef6\u5939\u8def\u5f84\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u5939\u8def\u5f84\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u8def\u5f84\u4e2d\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u60c5\u51b5\u3002\u4e3a\u4e86\u786e\u4fdd\u8def\u5f84\u7684\u6b63\u786e\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>normpath()<\/code>\u51fd\u6570\u6765\u89c4\u8303\u5316\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5b9a\u4e49\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &quot;example_folder\\\\sub_folder\/..\/sub_folder2&quot;<\/p>\n<h2><strong>\u4f7f\u7528os.path.normpath()\u89c4\u8303\u5316\u8def\u5f84<\/strong><\/h2>\n<p>normalized_path = os.path.normpath(folder_path)<\/p>\n<h2><strong>\u4f7f\u7528os.makedirs()\u65b0\u5efa\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>try:<\/p>\n<p>    os.makedirs(normalized_path, exist_ok=True)<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u5939 &#39;{normalized_path}&#39; \u65b0\u5efa\u6210\u529f&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u65b0\u5efa\u6587\u4ef6\u5939\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u6587\u4ef6\u5939\u8def\u5f84<code>folder_path<\/code>\uff0c\u5e76\u4f7f\u7528<code>os.path.normpath()<\/code>\u51fd\u6570\u5bf9\u8def\u5f84\u8fdb\u884c\u4e86\u89c4\u8303\u5316\u5904\u7406\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528<code>os.makedirs()<\/code>\u51fd\u6570\u65b0\u5efa\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u8de8\u5e73\u53f0\u6587\u4ef6\u5939\u8def\u5f84\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u8de8\u5e73\u53f0\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u6587\u4ef6\u5939\u8def\u5f84\u7684\u5904\u7406\u65b9\u5f0f\u517c\u5bb9\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>join()<\/code>\u51fd\u6570\u6765\u6784\u5efa\u8de8\u5e73\u53f0\u7684\u6587\u4ef6\u5939\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5b9a\u4e49\u6587\u4ef6\u5939\u8def\u5f84\u7684\u5404\u4e2a\u90e8\u5206<\/strong><\/h2>\n<p>folder_parts = [&quot;example_folder&quot;, &quot;sub_folder&quot;]<\/p>\n<h2><strong>\u4f7f\u7528os.path.join()\u6784\u5efa\u8de8\u5e73\u53f0\u7684\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = os.path.join(*folder_parts)<\/p>\n<h2><strong>\u4f7f\u7528os.makedirs()\u65b0\u5efa\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>try:<\/p>\n<p>    os.makedirs(folder_path, exist_ok=True)<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u5939 &#39;{folder_path}&#39; \u65b0\u5efa\u6210\u529f&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u65b0\u5efa\u6587\u4ef6\u5939\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u6587\u4ef6\u5939\u8def\u5f84\u7684\u5404\u4e2a\u90e8\u5206<code>folder_parts<\/code>\uff0c\u5e76\u4f7f\u7528<code>os.path.join()<\/code>\u51fd\u6570\u5c06\u5b83\u4eec\u7ec4\u5408\u6210\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u6587\u4ef6\u5939\u8def\u5f84\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528<code>os.makedirs()<\/code>\u51fd\u6570\u65b0\u5efa\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u5982\u4f55\u65b0\u5efa\u6587\u4ef6\u5939\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>os.mkdir()<\/code>\u548c<code>os.makedirs()<\/code>\u51fd\u6570\u3001\u5904\u7406\u6587\u4ef6\u5939\u5df2\u5b58\u5728\u7684\u60c5\u51b5\u3001\u4f7f\u7528<code>Pathlib<\/code>\u6a21\u5757\u3001\u7ed3\u5408\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3001\u5904\u7406\u8def\u5f84\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u4ee5\u53ca\u8de8\u5e73\u53f0\u6587\u4ef6\u5939\u8def\u5f84\u5904\u7406\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e2d\u7075\u6d3b\u5730\u65b0\u5efa\u6587\u4ef6\u5939\uff0c\u5e76\u786e\u4fdd\u8def\u5f84\u7684\u6b63\u786e\u6027\u548c\u517c\u5bb9\u6027\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\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>mkdir()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nos.mkdir(&#39;\u65b0\u7684\u6587\u4ef6\u5939\u540d&#39;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u60a8\u63d0\u4f9b\u7684\u6587\u4ef6\u5939\u540d\u79f0\u662f\u552f\u4e00\u7684\uff0c\u4ee5\u907f\u514d\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u521b\u5efa\u591a\u5c42\u6587\u4ef6\u5939\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u521b\u5efa\u591a\u5c42\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.makedirs()<\/code>\u51fd\u6570\u3002\u8fd9\u5c06\u5141\u8bb8\u60a8\u4e00\u6b21\u6027\u521b\u5efa\u591a\u4e2a\u5b50\u6587\u4ef6\u5939\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nos.makedirs(&#39;\u7236\u6587\u4ef6\u5939\/\u5b50\u6587\u4ef6\u5939\/\u5b59\u6587\u4ef6\u5939&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u4f1a\u5728\u6307\u5b9a\u8def\u5f84\u4e0b\u521b\u5efa\u6240\u6709\u5fc5\u8981\u7684\u6587\u4ef6\u5939\u3002<\/p>\n<p><strong>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\u5982\u4f55\u5904\u7406\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>mkdir()<\/code>\u6216<code>makedirs()<\/code>\u65f6\uff0c\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u5df2\u7ecf\u5b58\u5728\uff0c\u5c06\u4f1a\u5f15\u53d1<code>FileExistsError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u9519\u8bef\uff0c\u60a8\u53ef\u4ee5\u5728\u521b\u5efa\u6587\u4ef6\u5939\u4e4b\u524d\u68c0\u67e5\u5b83\u662f\u5426\u5b58\u5728\uff0c\u5982\u4e0b\u6240\u793a\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_name = &#39;\u65b0\u7684\u6587\u4ef6\u5939\u540d&#39;\nif not os.path.exists(folder_name):\n    os.mkdir(folder_name)\nelse:\n    print(&quot;\u6587\u4ef6\u5939\u5df2\u7ecf\u5b58\u5728\uff01&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528os\u6a21\u5757\u4e2d\u7684mkdir()\u6216makedirs()\u51fd\u6570\u6765\u5b9e\u73b0\u3001\u8fd9\u4e24\u79cd [&hellip;]","protected":false},"author":3,"featured_media":1127050,"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\/1127044"}],"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=1127044"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127044\/revisions"}],"predecessor-version":[{"id":1127055,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127044\/revisions\/1127055"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1127050"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1127044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1127044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1127044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}