{"id":987405,"date":"2024-12-27T07:53:16","date_gmt":"2024-12-26T23:53:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/987405.html"},"modified":"2024-12-27T07:53:18","modified_gmt":"2024-12-26T23:53:18","slug":"%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%bapython%e7%9b%ae%e5%bd%95%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/987405.html","title":{"rendered":"\u5982\u4f55\u521b\u5efapython\u76ee\u5f55\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063536\/d5e9cbdc-5d69-4f08-a304-4d1d4592c7a4.webp\" alt=\"\u5982\u4f55\u521b\u5efapython\u76ee\u5f55\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u521b\u5efaPython\u76ee\u5f55\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u6216\u624b\u52a8\u521b\u5efa\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002os\u6a21\u5757\u63d0\u4f9b\u4e86\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u7684\u529f\u80fd\u3001pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3001\u624b\u52a8\u521b\u5efa\u5219\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u7b49\u624b\u52a8\u64cd\u4f5c\u3002<\/strong>\u5728\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528os\u6a21\u5757\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u800cpathlib\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u548c\u4fbf\u6377\u7684\u64cd\u4f5c\u9009\u9879\u3002\u624b\u52a8\u521b\u5efa\u5219\u9002\u5408\u4e8e\u4e0d\u9700\u8981\u7f16\u5199\u4ee3\u7801\u7684\u7b80\u5355\u9700\u6c42\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528OS\u6a21\u5757<\/p>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\u3002\u901a\u8fc7os\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u76ee\u5f55\u3001\u6587\u4ef6\u4ee5\u53ca\u8fdb\u884c\u5176\u4ed6\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165os\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528os\u6a21\u5757\u8fdb\u884c\u76ee\u5f55\u64cd\u4f5c\u65f6\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>os.makedirs()<\/code>\u53ef\u4ee5\u9012\u5f52\u5730\u521b\u5efa\u76ee\u5f55\u3002\u5982\u679c\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">directory = &quot;path\/to\/directory&quot;<\/p>\n<p>os.makedirs(directory, exist_ok=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>exist_ok=True<\/code>\u53c2\u6570\u786e\u4fdd\u5373\u4f7f\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u7a0b\u5e8f\u4e5f\u4e0d\u4f1a\u62a5\u9519\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u76ee\u5f55\u4e4b\u524d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists()<\/code>\u8fdb\u884c\u68c0\u67e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if not os.path.exists(directory):<\/p>\n<p>    os.makedirs(directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5220\u9664\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5220\u9664\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.rmdir()<\/code>\u51fd\u6570\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u8be5\u76ee\u5f55\u5fc5\u987b\u4e3a\u7a7a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">os.rmdir(directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528PATHLIB\u6a21\u5757<\/p>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u65b0\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u3002\u76f8\u6bd4\u4e8eos\u6a21\u5757\uff0cpathlib\u7684\u8bed\u6cd5\u66f4\u7b80\u6d01\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165pathlib\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528pathlib\u6a21\u5757\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>Path.mkdir()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">directory = Path(&quot;path\/to\/directory&quot;)<\/p>\n<p>directory.mkdir(parents=True, exist_ok=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>parents=True<\/code>\u53c2\u6570\u4f1a\u9012\u5f52\u521b\u5efa\u76ee\u5f55\uff0c<code>exist_ok=True<\/code>\u5219\u786e\u4fdd\u76ee\u5f55\u5b58\u5728\u65f6\u4e0d\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>Path.exists()<\/code>\u65b9\u6cd5\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if not directory.exists():<\/p>\n<p>    directory.mkdir(parents=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5220\u9664\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>Path.rmdir()<\/code>\u65b9\u6cd5\u5220\u9664\u76ee\u5f55\uff0c\u4f46\u76ee\u5f55\u5fc5\u987b\u4e3a\u7a7a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">directory.rmdir()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u624b\u52a8\u521b\u5efa\u76ee\u5f55<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u901a\u8fc7\u4ee3\u7801\u521b\u5efa\u76ee\u5f55\uff0c\u53ef\u4ee5\u901a\u8fc7\u64cd\u4f5c\u7cfb\u7edf\u7684\u547d\u4ee4\u884c\u6216\u56fe\u5f62\u754c\u9762\u624b\u52a8\u521b\u5efa\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u547d\u4ee4\u884c\u521b\u5efa<\/strong><\/p>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>mkdir<\/code>\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">mkdir path\\to\\directory<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Linux\u6216Mac\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">mkdir -p path\/to\/directory<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u5f62\u754c\u9762\u521b\u5efa<\/strong><\/p>\n<\/p>\n<p><p>\u6253\u5f00\u4f60\u7684\u6587\u4ef6\u7ba1\u7406\u5668\uff08\u5982Windows<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u5668\u3001Mac Finder\u7b49\uff09\uff0c\u5bfc\u822a\u5230\u4f60\u60f3\u8981\u521b\u5efa\u76ee\u5f55\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u53f3\u952e\u9009\u62e9\u201c\u65b0\u5efa\u6587\u4ef6\u5939\u201d\u6216\u7c7b\u4f3c\u9009\u9879\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u7075\u6d3b\u9009\u62e9\u9002\u5408\u7684\u65b9\u5f0f\u521b\u5efaPython\u76ee\u5f55\u6587\u4ef6\u3002\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u4e2a\u4eba\u4e60\u60ef\uff0c\u53ef\u4ee5\u9009\u62e9\u4ee3\u7801\u5b9e\u73b0\u6216\u8005\u624b\u52a8\u521b\u5efa\u7684\u65b9\u5f0f\uff0c\u786e\u4fdd\u76ee\u5f55\u7ed3\u6784\u7b26\u5408\u9879\u76ee\u8981\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>mkdir()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>os.mkdir(&#39;\u76ee\u5f55\u540d&#39;)<\/code>\u6765\u521b\u5efa\u76ee\u6807\u76ee\u5f55\u3002\u5982\u679c\u4f60\u9700\u8981\u521b\u5efa\u591a\u5c42\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.makedirs(&#39;\u8def\u5f84\/\u5230\/\u76ee\u5f55&#39;)<\/code>\u3002<\/p>\n<p><strong>\u5728\u521b\u5efa\u76ee\u5f55\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5df2\u5b58\u5728\u7684\u76ee\u5f55\uff1f<\/strong><br \/>\u5728\u5c1d\u8bd5\u521b\u5efa\u76ee\u5f55\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists(&#39;\u76ee\u5f55\u540d&#39;)<\/code>\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u3002\u5982\u679c\u76ee\u5f55\u5df2\u5b58\u5728\uff0c\u53ef\u4ee5\u9009\u62e9\u4e0d\u521b\u5efa\u65b0\u76ee\u5f55\u3001\u5220\u9664\u65e7\u76ee\u5f55\u6216\u8986\u76d6\u5b83\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u4e3a\u65b0\u521b\u5efa\u7684\u76ee\u5f55\u8bbe\u7f6e\u6743\u9650\uff1f<\/strong><br \/>\u4f7f\u7528<code>os.mkdir()<\/code>\u6216<code>os.makedirs()<\/code>\u521b\u5efa\u76ee\u5f55\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.chmod(&#39;\u76ee\u5f55\u540d&#39;, \u6743\u9650)<\/code>\u6765\u8bbe\u7f6e\u8be5\u76ee\u5f55\u7684\u6743\u9650\u3002\u6743\u9650\u53c2\u6570\u901a\u5e38\u4f7f\u7528\u516b\u8fdb\u5236\u8868\u793a\u6cd5\uff0c\u5982<code>0o755<\/code>\uff0c\u8fd9\u5c06\u5141\u8bb8\u6240\u6709\u8005\u8bfb\u3001\u5199\u548c\u6267\u884c\uff0c\u7ec4\u7528\u6237\u548c\u5176\u4ed6\u7528\u6237\u53ea\u5141\u8bb8\u8bfb\u548c\u6267\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u521b\u5efaPython\u76ee\u5f55\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u6216\u624b\u52a8\u521b\u5efa\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002os\u6a21\u5757\u63d0\u4f9b\u4e86\u64cd\u4f5c\u7cfb\u7edf [&hellip;]","protected":false},"author":3,"featured_media":987411,"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\/987405"}],"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=987405"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/987405\/revisions"}],"predecessor-version":[{"id":987415,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/987405\/revisions\/987415"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/987411"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=987405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=987405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=987405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}