{"id":1132429,"date":"2025-01-08T20:57:06","date_gmt":"2025-01-08T12:57:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132429.html"},"modified":"2025-01-08T20:57:08","modified_gmt":"2025-01-08T12:57:08","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e8%b7%af%e5%be%84%e4%b8%8b%e5%88%9b%e5%bb%ba%e6%96%87%e4%bb%b6%e5%a4%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132429.html","title":{"rendered":"python\u5982\u4f55\u5728\u8def\u5f84\u4e0b\u521b\u5efa\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\/25101957\/03495170-1ff0-4616-8640-2c464e16eae6.webp\" alt=\"python\u5982\u4f55\u5728\u8def\u5f84\u4e0b\u521b\u5efa\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>Python\u5728\u8def\u5f84\u4e0b\u521b\u5efa\u6587\u4ef6\u5939\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001\u4ee5\u53cashutil\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662fos\u6a21\u5757\u548cpathlib\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u7b80\u5355\u4e14\u76f4\u63a5\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u6587\u4ef6\u5939\u3002<strong>os\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u4f20\u7edf\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570Python\u7248\u672c\uff0c\u800cpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u51e0\u79cd\u65b9\u6cd5\u5728Python\u4e2d\u521b\u5efa\u6587\u4ef6\u5939\uff0c\u5e76\u63a2\u8ba8\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757\u521b\u5efa\u6587\u4ef6\u5939<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u65b9\u6cd5\u3002\u8981\u521b\u5efa\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u4e2d\u7684<code>os.makedirs()<\/code>\u6216<code>os.mkdir()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001os.mkdir()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>os.mkdir()<\/code>\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u5355\u4e2a\u76ee\u5f55\u3002\u5982\u679c\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1FileExistsError\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def create_single_directory(path):<\/p>\n<p>    try:<\/p>\n<p>        os.mkdir(path)<\/p>\n<p>        print(f&quot;Directory &#39;{path}&#39; created successfully&quot;)<\/p>\n<p>    except FileExistsError:<\/p>\n<p>        print(f&quot;Directory &#39;{path}&#39; already exists&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>create_single_directory(&#39;example_dir&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001os.makedirs()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>os.makedirs()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u9012\u5f52\u5730\u521b\u5efa\u76ee\u5f55\u6811\u3002\u5982\u679c\u7236\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5b83\u4f1a\u81ea\u52a8\u521b\u5efa\u6240\u9700\u7684\u6240\u6709\u7236\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def create_directories(path):<\/p>\n<p>    try:<\/p>\n<p>        os.makedirs(path, exist_ok=True)<\/p>\n<p>        print(f&quot;Directories &#39;{path}&#39; created successfully&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>create_directories(&#39;parent_dir\/child_dir&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u4f7f\u7528os\u6a21\u5757\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li><strong>\u8def\u5f84\u7684\u683c\u5f0f<\/strong>\uff1a\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8def\u5f84\u7684\u683c\u5f0f\u53ef\u80fd\u4e0d\u540c\u3002Windows\u4f7f\u7528\u53cd\u659c\u6760\uff08<code><\/code>\uff09\uff0c\u800cUnix\u548cLinux\u7cfb\u7edf\u4f7f\u7528\u6b63\u659c\u6760\uff08<code>\/<\/code>\uff09\u3002<\/li>\n<li><strong>\u6743\u9650\u95ee\u9898<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u624d\u80fd\u521b\u5efa\u76ee\u5f55\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757\u521b\u5efa\u6587\u4ef6\u5939<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u662f\u5728Python 3.4\u4e2d\u5f15\u5165\u7684\uff0c\u5b83\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002<code>pathlib<\/code>\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u76f4\u89c2\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>2\u3001Path.mkdir()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>Path.mkdir()<\/code>\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u5355\u4e2a\u76ee\u5f55\u3002\u5982\u679c\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1FileExistsError\uff0c\u9664\u975e\u8bbe\u7f6e\u53c2\u6570<code>exist_ok=True<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def create_single_directory(path):<\/p>\n<p>    try:<\/p>\n<p>        Path(path).mkdir(exist_ok=True)<\/p>\n<p>        print(f&quot;Directory &#39;{path}&#39; created successfully&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>create_single_directory(&#39;example_dir&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001Path.mkdir()\u65b9\u6cd5\u7684\u9012\u5f52\u521b\u5efa<\/h3>\n<\/p>\n<p><p><code>Path.mkdir()<\/code>\u4e5f\u53ef\u4ee5\u9012\u5f52\u521b\u5efa\u76ee\u5f55\uff0c\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570<code>parents=True<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def create_directories(path):<\/p>\n<p>    try:<\/p>\n<p>        Path(path).mkdir(parents=True, exist_ok=True)<\/p>\n<p>        print(f&quot;Directories &#39;{path}&#39; created successfully&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>create_directories(&#39;parent_dir\/child_dir&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u4f7f\u7528pathlib\u6a21\u5757\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<ul>\n<li><strong>\u9762\u5411\u5bf9\u8c61<\/strong>\uff1a<code>pathlib<\/code>\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u6613\u8bfb\u3002<\/li>\n<li><strong>\u8de8\u5e73\u53f0<\/strong>\uff1a<code>pathlib<\/code>\u81ea\u52a8\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p><h2>\u4e09\u3001\u4f7f\u7528shutil\u6a21\u5757\u521b\u5efa\u6587\u4ef6\u5939<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p><code>shutil<\/code>\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u4f8b\u5982\u590d\u5236\u3001\u79fb\u52a8\u3001\u5220\u9664\u7b49\u3002\u5c3d\u7ba1<code>shutil<\/code>\u6a21\u5757\u4e0d\u76f4\u63a5\u63d0\u4f9b\u521b\u5efa\u76ee\u5f55\u7684\u65b9\u6cd5\uff0c\u4f46\u5b83\u5728\u5904\u7406\u590d\u6742\u6587\u4ef6\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7ed3\u5408os\u6a21\u5757\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408<code>os<\/code>\u6a21\u5757\u548c<code>shutil<\/code>\u6a21\u5757\u6765\u521b\u5efa\u76ee\u5f55\u5e76\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>def create_and_copy(source, destination):<\/p>\n<p>    try:<\/p>\n<p>        os.makedirs(destination, exist_ok=True)<\/p>\n<p>        shutil.copy(source, destination)<\/p>\n<p>        print(f&quot;File &#39;{source}&#39; copied to &#39;{destination}&#39; successfully&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>create_and_copy(&#39;example_file.txt&#39;, &#39;example_dir&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528shutil\u6a21\u5757\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u7ea7\u64cd\u4f5c<\/strong>\uff1a<code>shutil<\/code>\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u6267\u884c\u9ad8\u7ea7\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u4f8b\u5982\u590d\u5236\u3001\u79fb\u52a8\u548c\u5220\u9664\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u7ed3\u5408<code>os<\/code>\u6a21\u5757\u4f7f\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6587\u4ef6\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h2>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><h3>1\u3001\u8def\u5f84\u7684\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8def\u5f84\u7684\u5904\u7406\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u6216<code>pathlib<\/code>\u6a21\u5757\u6765\u5904\u7406\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_full_path(relative_path):<\/p>\n<p>    return os.path.abspath(os.path.join(os.getcwd(), relative_path))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(get_full_path(&#39;example_dir&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u76ee\u5f55\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\uff0c\u4f8b\u5982\u76ee\u5f55\u5df2\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u5efa\u8bae\u5728\u521b\u5efa\u76ee\u5f55\u65f6\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def create_directory_with_exception_handling(path):<\/p>\n<p>    try:<\/p>\n<p>        os.makedirs(path, exist_ok=True)<\/p>\n<p>        print(f&quot;Directory &#39;{path}&#39; created successfully&quot;)<\/p>\n<p>    except PermissionError:<\/p>\n<p>        print(f&quot;Permission denied: Unable to create directory &#39;{path}&#39;&quot;)<\/p>\n<p>    except FileExistsError:<\/p>\n<p>        print(f&quot;Directory &#39;{path}&#39; already exists&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>create_directory_with_exception_handling(&#39;example_dir&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8bb0\u5f55\u64cd\u4f5c\u65e5\u5fd7\u662f\u4e00\u4e2a\u826f\u597d\u7684\u4e60\u60ef\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>logging<\/code>\u6a21\u5757\u6765\u8bb0\u5f55\u521b\u5efa\u76ee\u5f55\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7<\/strong><\/h2>\n<p>logging.basicConfig(filename=&#39;directory_creation.log&#39;, level=logging.INFO)<\/p>\n<p>def create_directory_with_logging(path):<\/p>\n<p>    try:<\/p>\n<p>        os.makedirs(path, exist_ok=True)<\/p>\n<p>        logging.info(f&quot;Directory &#39;{path}&#39; created successfully&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>create_directory_with_logging(&#39;example_dir&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u6743\u9650\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e\u76ee\u5f55\u7684\u6743\u9650\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.chmod()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u76ee\u5f55\u7684\u6743\u9650\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import stat<\/p>\n<p>def create_directory_with_permissions(path):<\/p>\n<p>    try:<\/p>\n<p>        os.makedirs(path, exist_ok=True)<\/p>\n<p>        os.chmod(path, stat.S_IRWXU)  # \u8bbe\u7f6e\u6743\u9650\u4e3a\u7528\u6237\u8bfb\u3001\u5199\u3001\u6267\u884c<\/p>\n<p>        print(f&quot;Directory &#39;{path}&#39; created successfully with permissions&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>create_directory_with_permissions(&#39;example_dir&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001\u81ea\u52a8\u5316\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7f16\u5199\u81ea\u52a8\u5316\u811a\u672c\u6765\u521b\u5efa\u76ee\u5f55\u548c\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u811a\u672c\u6765\u521b\u5efa\u9879\u76ee\u76ee\u5f55\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def create_project_structure(base_path):<\/p>\n<p>    directories = [<\/p>\n<p>        &#39;src&#39;,<\/p>\n<p>        &#39;src\/components&#39;,<\/p>\n<p>        &#39;src\/utils&#39;,<\/p>\n<p>        &#39;tests&#39;,<\/p>\n<p>        &#39;docs&#39;<\/p>\n<p>    ]<\/p>\n<p>    for directory in directories:<\/p>\n<p>        path = os.path.join(base_path, directory)<\/p>\n<p>        try:<\/p>\n<p>            os.makedirs(path, exist_ok=True)<\/p>\n<p>            print(f&quot;Directory &#39;{path}&#39; created successfully&quot;)<\/p>\n<p>        except Exception as e:<\/p>\n<p>            print(f&quot;An error occurred: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>create_project_structure(&#39;my_project&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u521b\u5efa\u76ee\u5f55\u5e76\u5904\u7406\u5404\u79cd\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u95ee\u9898\u3002\u65e0\u8bba\u662f\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\uff0c\u8fd8\u662f\u7ed3\u5408shutil\u6a21\u5757\uff0c\u90fd\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u548c\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u7684\u76ee\u5f55\u521b\u5efa\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u591a\u5c42\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>makedirs()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u591a\u5c42\u6587\u4ef6\u5939\u3002\u8be5\u51fd\u6570\u4f1a\u68c0\u67e5\u8def\u5f84\u4e2d\u6bcf\u4e00\u5c42\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u521b\u5efa\u5b83\u4eec\u3002\u4f8b\u5982\uff0c<code>os.makedirs(&#39;parent_folder\/child_folder&#39;)<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>parent_folder<\/code>\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u5176\u5185\u521b\u5efa<code>child_folder<\/code>\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.makedirs()<\/code>\u65f6\uff0c\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u5df2\u7ecf\u5b58\u5728\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>exist_ok=True<\/code>\u53c2\u6570\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002\u8fd9\u6837\uff0c\u5982\u679c\u6587\u4ef6\u5939\u5df2\u7ecf\u5b58\u5728\uff0c\u7a0b\u5e8f\u5c06\u4e0d\u4f1a\u62a5\u9519\u3002\u4f8b\u5982\uff1a<code>os.makedirs(&#39;folder_name&#39;, exist_ok=True)<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7279\u5b9a\u8def\u5f84\u4e0b\u521b\u5efa\u6587\u4ef6\u5939\u5e76\u6307\u5b9a\u6743\u9650\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.mkdir()<\/code>\u6216<code>os.makedirs()<\/code>\u7684<code>mode<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u65b0\u521b\u5efa\u6587\u4ef6\u5939\u7684\u6743\u9650\u3002\u4f8b\u5982\uff0c<code>os.mkdir(&#39;new_folder&#39;, mode=0o755)<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>new_folder<\/code>\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u8bbe\u7f6e\u5176\u6743\u9650\u4e3a755\u3002\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","protected":false},"excerpt":{"rendered":"Python\u5728\u8def\u5f84\u4e0b\u521b\u5efa\u6587\u4ef6\u5939\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001\u4ee5\u53cashutil\u6a21\u5757\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1132436,"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\/1132429"}],"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=1132429"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132429\/revisions"}],"predecessor-version":[{"id":1132437,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132429\/revisions\/1132437"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132436"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}