{"id":1058060,"date":"2024-12-31T15:13:12","date_gmt":"2024-12-31T07:13:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058060.html"},"modified":"2024-12-31T15:13:15","modified_gmt":"2024-12-31T07:13:15","slug":"python%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98%e5%a4%9a%e4%b8%aa%e6%96%87%e4%bb%b6%e5%a4%b9%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1058060.html","title":{"rendered":"python\u5982\u4f55\u4fdd\u5b58\u591a\u4e2a\u6587\u4ef6\u5939\u4e2d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/ea68dadb-aa9c-4775-9982-aac3f134c53f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4fdd\u5b58\u591a\u4e2a\u6587\u4ef6\u5939\u4e2d\" \/><\/p>\n<p><p> <strong>Python\u4fdd\u5b58\u591a\u4e2a\u6587\u4ef6\u5939\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3001\u9012\u5f52\u521b\u5efa\u6587\u4ef6\u5939\u8def\u5f84\u3001\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4fdd\u5b58\u591a\u4e2a\u6587\u4ef6\u5939\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684os\u6a21\u5757\u3001pathlib\u6a21\u5757\uff0c\u9012\u5f52\u521b\u5efa\u6587\u4ef6\u5939\u8def\u5f84\u7b49\u3002<strong>\u5176\u4e2d\uff0cos\u6a21\u5757\u548cpathlib\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u975e\u5e38\u4e30\u5bcc\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u4f7f\u7528os\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u6587\u4ef6\u5939\u53ca\u5176\u5b50\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5355\u4e2a\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u7684<code>os.mkdir()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u53ea\u80fd\u521b\u5efa\u5355\u4e2a\u6587\u4ef6\u5939\uff0c\u4e0d\u80fd\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u5355\u4e2a\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.mkdir(&#39;folder1&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.makedirs()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f1a\u9012\u5f52\u5730\u521b\u5efa\u6240\u6709\u4e0d\u5b58\u5728\u7684\u7236\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.makedirs(&#39;folder1\/folder2\/folder3&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/h4>\n<\/p>\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\u4ee5\u907f\u514d\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>folder_path = &#39;folder1\/folder2\/folder3&#39;<\/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.makedirs(folder_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u65b0\u6a21\u5757\uff0c\u7528\u4e8e\u9762\u5411\u5bf9\u8c61\u5730\u5904\u7406\u8def\u5f84\u3002\u76f8\u6bd4\u4e8eos\u6a21\u5757\uff0cpathlib\u6a21\u5757\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5355\u4e2a\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528pathlib\u6a21\u5757\u7684<code>Path.mkdir()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u5355\u4e2a\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>Path(&#39;folder1&#39;).mkdir()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u5c06<code>parents<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3aTrue\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>Path(&#39;folder1\/folder2\/folder3&#39;).mkdir(parents=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/h4>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>folder_path = Path(&#39;folder1\/folder2\/folder3&#39;)<\/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(parents=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u9012\u5f52\u521b\u5efa\u6587\u4ef6\u5939\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u9012\u5f52\u5730\u521b\u5efa\u6587\u4ef6\u5939\u8def\u5f84\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def create_folders_recursively(path):<\/p>\n<p>    # \u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/p>\n<p>    if not os.path.exists(path):<\/p>\n<p>        # \u9012\u5f52\u521b\u5efa\u7236\u6587\u4ef6\u5939<\/p>\n<p>        create_folders_recursively(os.path.dirname(path))<\/p>\n<p>        # \u521b\u5efa\u5f53\u524d\u6587\u4ef6\u5939<\/p>\n<p>        os.mkdir(path)<\/p>\n<h2><strong>\u9012\u5f52\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>create_folders_recursively(&#39;folder1\/folder2\/folder3&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5f02\u5e38\u5904\u7406<\/h3>\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\u7ecf\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>folder_path = &#39;folder1\/folder2\/folder3&#39;<\/p>\n<p>try:<\/p>\n<p>    # \u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/p>\n<p>    os.makedirs(folder_path)<\/p>\n<p>except FileExistsError:<\/p>\n<p>    print(f&quot;The folder &#39;{folder_path}&#39; already exists.&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(f&quot;Permission denied to create &#39;{folder_path}&#39;.&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u521b\u5efa\u6587\u4ef6\u5939\u540e\u901a\u5e38\u8fd8\u9700\u8981\u5728\u6587\u4ef6\u5939\u4e2d\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\uff0c\u4f8b\u5982\u4fdd\u5b58\u6587\u4ef6\u3001\u8bfb\u53d6\u6587\u4ef6\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u521b\u5efa\u7684\u6587\u4ef6\u5939\u4e2d\u4fdd\u5b58\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>folder_path = &#39;folder1\/folder2\/folder3&#39;<\/p>\n<p>file_path = os.path.join(folder_path, &#39;example.txt&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.makedirs(folder_path, exist_ok=True)<\/p>\n<h2><strong>\u5728\u6587\u4ef6\u5939\u4e2d\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, world!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4fdd\u5b58\u591a\u4e2a\u6587\u4ef6\u5939\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u7f16\u5199\u9012\u5f52\u51fd\u6570\u6765\u521b\u5efa\u591a\u7ea7\u6587\u4ef6\u5939\uff0c\u5e76\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u8fd8\u9700\u8981\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\uff0c\u5728\u521b\u5efa\u7684\u6587\u4ef6\u5939\u4e2d\u8fdb\u884c\u6587\u4ef6\u7684\u4fdd\u5b58\u548c\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u7ba1\u7406\u6587\u4ef6\u5939\u548c\u6587\u4ef6\uff0c\u63d0\u5347Python\u7f16\u7a0b\u7684\u6548\u7387\u548c\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6279\u91cf\u4fdd\u5b58\u6587\u4ef6\u5230\u591a\u4e2a\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528os\u548cshutil\u5e93\u6765\u6279\u91cf\u4fdd\u5b58\u6587\u4ef6\u5230\u591a\u4e2a\u6587\u4ef6\u5939\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b9a\u4e49\u76ee\u6807\u6587\u4ef6\u5939\u7684\u8def\u5f84\uff0c\u5e76\u4f7f\u7528os.makedirs()\u51fd\u6570\u521b\u5efa\u8fd9\u4e9b\u6587\u4ef6\u5939\u3002\u63a5\u7740\uff0c\u5229\u7528shutil.copy()\u6216shutil.move()\u51fd\u6570\u5c06\u6587\u4ef6\u590d\u5236\u6216\u79fb\u52a8\u5230\u76f8\u5e94\u7684\u6587\u4ef6\u5939\u4e2d\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6587\u4ef6\u6574\u7406\u5230\u4e0d\u540c\u7684\u76ee\u5f55\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u6587\u4ef6\u6309\u7167\u7279\u5b9a\u6761\u4ef6\u81ea\u52a8\u5206\u7c7b\u4fdd\u5b58\u5230\u591a\u4e2a\u6587\u4ef6\u5939\u4e2d\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u6761\u4ef6\u8bed\u53e5\u6765\u5224\u65ad\u6587\u4ef6\u7684\u5c5e\u6027\uff08\u5982\u6587\u4ef6\u7c7b\u578b\u3001\u5927\u5c0f\u7b49\uff09\uff0c\u5e76\u6839\u636e\u8fd9\u4e9b\u6761\u4ef6\u5c06\u6587\u4ef6\u4fdd\u5b58\u5230\u4e0d\u540c\u7684\u6587\u4ef6\u5939\u4e2d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5229\u7528os.listdir()\u83b7\u53d6\u6587\u4ef6\u5217\u8868\uff0c\u5e76\u7ed3\u5408if\u6761\u4ef6\u8bed\u53e5\u5c06\u6587\u4ef6\u5206\u7c7b\uff0c\u4f7f\u7528shutil\u5e93\u5c06\u5176\u79fb\u52a8\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u5939\u3002<\/p>\n<p><strong>\u5728\u4fdd\u5b58\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u540d\u51b2\u7a81\u95ee\u9898\uff1f<\/strong><br \/>\u5904\u7406\u6587\u4ef6\u540d\u51b2\u7a81\u53ef\u4ee5\u901a\u8fc7\u5728\u6587\u4ef6\u540d\u4e2d\u6dfb\u52a0\u65f6\u95f4\u6233\u3001\u5e8f\u53f7\u6216\u5176\u4ed6\u552f\u4e00\u6807\u8bc6\u7b26\u6765\u5b9e\u73b0\u3002\u4f7f\u7528Python\u7684datetime\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u4fdd\u5b58\u6587\u4ef6\u65f6\u4e3a\u6587\u4ef6\u540d\u6dfb\u52a0\u8fd9\u4e9b\u4fe1\u606f\uff0c\u4ee5\u907f\u514d\u4e0e\u5df2\u5b58\u5728\u6587\u4ef6\u53d1\u751f\u51b2\u7a81\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4fdd\u5b58\u591a\u4e2a\u6587\u4ef6\u5939\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3001\u9012\u5f52\u521b\u5efa\u6587\u4ef6\u5939\u8def\u5f84\u3001\u5f02\u5e38\u5904\u7406 \u5728Pyt [&hellip;]","protected":false},"author":3,"featured_media":1058070,"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\/1058060"}],"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=1058060"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058060\/revisions"}],"predecessor-version":[{"id":1058071,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058060\/revisions\/1058071"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1058070"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1058060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1058060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1058060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}