{"id":1094609,"date":"2025-01-08T14:42:43","date_gmt":"2025-01-08T06:42:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094609.html"},"modified":"2025-01-08T14:42:45","modified_gmt":"2025-01-08T06:42:45","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6%e5%a4%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094609.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5199\u5165\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\/24210636\/6b8d0123-b809-4f98-b1d9-b80d2713445a.webp\" alt=\"python\u4e2d\u5982\u4f55\u5199\u5165\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5199\u5165\u6587\u4ef6\u5939\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001\u4ee5\u53cashutil\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0cos\u6a21\u5757\u6700\u4e3a\u5e38\u7528\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u51fd\u6570\u3002pathlib\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u5316\u3001\u66f4\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u65b9\u6cd5\u3002\u800cshutil\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u6587\u4ef6\u590d\u5236\u3001\u79fb\u52a8\u7b49\u64cd\u4f5c\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u6a21\u5757\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u51fd\u6570\u3002\u4f7f\u7528os\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u521b\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u521b\u5efa\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528os\u6a21\u5757\u521b\u5efa\u6587\u4ef6\u5939\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8c03\u7528os.mkdir()\u51fd\u6570\u5373\u53ef\u3002os.mkdir()\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u521b\u5efa\u7684\u6587\u4ef6\u5939\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cmy_folder\u201d\u7684\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.mkdir(&quot;my_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8981\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528os.makedirs()\u51fd\u6570\u3002os.makedirs()\u51fd\u6570\u53ef\u4ee5\u9012\u5f52\u5730\u521b\u5efa\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\u201cparent_folder\/child_folder\u201d<\/strong><\/h2>\n<p>os.makedirs(&quot;parent_folder\/child_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5220\u9664\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528os\u6a21\u5757\u5220\u9664\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u8c03\u7528os.rmdir()\u51fd\u6570\u3002os.rmdir()\u51fd\u6570\u53ea\u80fd\u5220\u9664\u7a7a\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5220\u9664\u540d\u4e3a\u201cmy_folder\u201d\u7684\u7a7a\u76ee\u5f55<\/strong><\/h2>\n<p>os.rmdir(&quot;my_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8981\u5220\u9664\u975e\u7a7a\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528os.removedirs()\u51fd\u6570\u3002os.removedirs()\u51fd\u6570\u53ef\u4ee5\u9012\u5f52\u5730\u5220\u9664\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5220\u9664\u975e\u7a7a\u76ee\u5f55\u201cparent_folder\/child_folder\u201d<\/strong><\/h2>\n<p>os.removedirs(&quot;parent_folder\/child_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f7f\u7528pathlib\u6a21\u5757\u53ef\u4ee5\u66f4\u52a0\u65b9\u4fbf\u5730\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u521b\u5efa\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pathlib\u6a21\u5757\u521b\u5efa\u6587\u4ef6\u5939\uff0c\u53ea\u9700\u8c03\u7528Path\u5bf9\u8c61\u7684mkdir()\u65b9\u6cd5\u5373\u53ef\u3002mkdir()\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u521b\u5efa\u7684\u6587\u4ef6\u5939\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cmy_folder\u201d\u7684\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>Path(&quot;my_folder&quot;).mkdir()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8981\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528parents\u53c2\u6570\u3002\u5c06parents\u53c2\u6570\u8bbe\u7f6e\u4e3aTrue\uff0c\u53ef\u4ee5\u9012\u5f52\u5730\u521b\u5efa\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\u201cparent_folder\/child_folder\u201d<\/strong><\/h2>\n<p>Path(&quot;parent_folder\/child_folder&quot;).mkdir(parents=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5220\u9664\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pathlib\u6a21\u5757\u5220\u9664\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u8c03\u7528Path\u5bf9\u8c61\u7684rmdir()\u65b9\u6cd5\u3002rmdir()\u65b9\u6cd5\u53ea\u80fd\u5220\u9664\u7a7a\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u5220\u9664\u540d\u4e3a\u201cmy_folder\u201d\u7684\u7a7a\u76ee\u5f55<\/strong><\/h2>\n<p>Path(&quot;my_folder&quot;).rmdir()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8981\u5220\u9664\u975e\u7a7a\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528shutil\u6a21\u5757\u63d0\u4f9b\u7684rmtree()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import shutil<\/p>\n<h2><strong>\u5220\u9664\u975e\u7a7a\u76ee\u5f55\u201cparent_folder\/child_folder\u201d<\/strong><\/h2>\n<p>shutil.rmtree(Path(&quot;parent_folder\/child_folder&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001\u4f7f\u7528shutil\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>shutil\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u5982\u6587\u4ef6\u590d\u5236\u3001\u79fb\u52a8\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u3002\u4f7f\u7528shutil\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u590d\u5236\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528shutil\u6a21\u5757\u590d\u5236\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u8c03\u7528copytree()\u51fd\u6570\u3002copytree()\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f\u6e90\u76ee\u5f55\u8def\u5f84\u548c\u76ee\u6807\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<h2><strong>\u590d\u5236\u76ee\u5f55\u201csource_folder\u201d\u5230\u201cdestination_folder\u201d<\/strong><\/h2>\n<p>shutil.copytree(&quot;source_folder&quot;, &quot;destination_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u79fb\u52a8\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528shutil\u6a21\u5757\u79fb\u52a8\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u8c03\u7528move()\u51fd\u6570\u3002move()\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f\u6e90\u76ee\u5f55\u8def\u5f84\u548c\u76ee\u6807\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<h2><strong>\u79fb\u52a8\u76ee\u5f55\u201csource_folder\u201d\u5230\u201cdestination_folder\u201d<\/strong><\/h2>\n<p>shutil.move(&quot;source_folder&quot;, &quot;destination_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5220\u9664\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528shutil\u6a21\u5757\u5220\u9664\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u8c03\u7528rmtree()\u51fd\u6570\u3002rmtree()\u51fd\u6570\u53ef\u4ee5\u9012\u5f52\u5730\u5220\u9664\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<h2><strong>\u5220\u9664\u76ee\u5f55\u201cmy_folder\u201d\u53ca\u5176\u5185\u5bb9<\/strong><\/h2>\n<p>shutil.rmtree(&quot;my_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u6587\u4ef6\u5939\u6743\u9650<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\uff0c\u6709\u65f6\u9700\u8981\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u6743\u9650\u3002\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u90fd\u53ef\u4ee5\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u6743\u9650\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528os\u6a21\u5757\u8bbe\u7f6e\u6587\u4ef6\u5939\u6743\u9650<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7os.mkdir()\u51fd\u6570\u7684mode\u53c2\u6570\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u6743\u9650\u3002mode\u53c2\u6570\u662f\u4e00\u4e2a\u516b\u8fdb\u5236\u6570\uff0c\u8868\u793a\u6587\u4ef6\u5939\u7684\u6743\u9650\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cmy_folder\u201d\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u8bbe\u7f6e\u6743\u9650\u4e3a755<\/strong><\/h2>\n<p>os.mkdir(&quot;my_folder&quot;, mode=0o755)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528pathlib\u6a21\u5757\u8bbe\u7f6e\u6587\u4ef6\u5939\u6743\u9650<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7Path\u5bf9\u8c61\u7684chmod()\u65b9\u6cd5\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u6743\u9650\u3002chmod()\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u6743\u9650\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cmy_folder\u201d\u7684\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>folder = Path(&quot;my_folder&quot;)<\/p>\n<p>folder.mkdir()<\/p>\n<h2><strong>\u8bbe\u7f6e\u6587\u4ef6\u5939\u6743\u9650\u4e3a755<\/strong><\/h2>\n<p>folder.chmod(0o755)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e94\u3001\u6587\u4ef6\u5939\u5b58\u5728\u6027\u68c0\u67e5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u64cd\u4f5c\u6587\u4ef6\u5939\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u90fd\u53ef\u4ee5\u8fdb\u884c\u6587\u4ef6\u5939\u5b58\u5728\u6027\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528os\u6a21\u5757\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7os.path.exists()\u51fd\u6570\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002os.path.exists()\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u6587\u4ef6\u5939\u8def\u5f84\u3002\u5982\u679c\u6587\u4ef6\u5939\u5b58\u5728\uff0c\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u201cmy_folder\u201d\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(&quot;my_folder&quot;):<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5939\u5df2\u5b58\u5728&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5939\u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528pathlib\u6a21\u5757\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7Path\u5bf9\u8c61\u7684exists()\u65b9\u6cd5\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002exists()\u65b9\u6cd5\u4e0d\u63a5\u53d7\u53c2\u6570\u3002\u5982\u679c\u6587\u4ef6\u5939\u5b58\u5728\uff0c\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u201cmy_folder\u201d\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if Path(&quot;my_folder&quot;).exists():<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5939\u5df2\u5b58\u5728&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5939\u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u516d\u3001\u6587\u4ef6\u5939\u5185\u5bb9\u904d\u5386<\/strong><\/p>\n<\/p>\n<p><p>\u904d\u5386\u6587\u4ef6\u5939\u5185\u5bb9\u662f\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u4e2d\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u90fd\u53ef\u4ee5\u65b9\u4fbf\u5730\u904d\u5386\u6587\u4ef6\u5939\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528os\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7os.listdir()\u51fd\u6570\u904d\u5386\u6587\u4ef6\u5939\u5185\u5bb9\u3002os.listdir()\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u6587\u4ef6\u5939\u8def\u5f84\uff0c\u8fd4\u56de\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u904d\u5386\u6587\u4ef6\u5939\u201cmy_folder\u201d\u5185\u5bb9<\/strong><\/h2>\n<p>for item in os.listdir(&quot;my_folder&quot;):<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528pathlib\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7Path\u5bf9\u8c61\u7684iterdir()\u65b9\u6cd5\u904d\u5386\u6587\u4ef6\u5939\u5185\u5bb9\u3002iterdir()\u65b9\u6cd5\u4e0d\u63a5\u53d7\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7528\u4e8e\u8fed\u4ee3\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u904d\u5386\u6587\u4ef6\u5939\u201cmy_folder\u201d\u5185\u5bb9<\/strong><\/h2>\n<p>for item in Path(&quot;my_folder&quot;).iterdir():<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5199\u5165\u6587\u4ef6\u5939\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001\u4ee5\u53cashutil\u6a21\u5757\u3002os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u51fd\u6570\uff0cpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u5316\u3001\u66f4\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u65b9\u6cd5\uff0c\u800cshutil\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u521b\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\u3001\u590d\u5236\u3001\u79fb\u52a8\u3001\u6743\u9650\u8bbe\u7f6e\u3001\u5b58\u5728\u6027\u68c0\u67e5\u3001\u5185\u5bb9\u904d\u5386\u7b49\u64cd\u4f5c\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\u65b0\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>mkdir()<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u65b0\u6587\u4ef6\u5939\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>os.mkdir(&#39;\u6587\u4ef6\u5939\u540d\u79f0&#39;)<\/code>\u6765\u521b\u5efa\u6587\u4ef6\u5939\u3002\u5982\u679c\u9700\u8981\u521b\u5efa\u591a\u5c42\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.makedirs(&#39;\u7236\u6587\u4ef6\u5939\/\u5b50\u6587\u4ef6\u5939&#39;)<\/code>\u3002<\/p>\n<p><strong>Python\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84\uff1f<\/strong><br \/>\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5b8c\u6574\u8def\u5f84\u6765\u9009\u62e9\u6587\u4ef6\u5939\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>open(&#39;\u6587\u4ef6\u5939\u8def\u5f84\/\u6587\u4ef6\u540d\u79f0.txt&#39;, &#39;w&#39;)<\/code>\u6765\u5728\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u6216\u5199\u5165\u6587\u4ef6\u3002\u786e\u4fdd\u6587\u4ef6\u5939\u8def\u5f84\u5b58\u5728\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u5e76\u907f\u514d\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u5199\u5165\u6587\u4ef6\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists(&#39;\u6587\u4ef6\u5939\u8def\u5f84&#39;)<\/code>\u6765\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u9009\u62e9\u521b\u5efa\u8be5\u6587\u4ef6\u5939\uff0c\u786e\u4fdd\u5199\u5165\u64cd\u4f5c\u987a\u5229\u8fdb\u884c\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5728\u5c1d\u8bd5\u5199\u5165\u6587\u4ef6\u65f6\u51fa\u73b0\u627e\u4e0d\u5230\u8def\u5f84\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5199\u5165\u6587\u4ef6\u5939\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001\u4ee5\u53cashutil\u6a21\u5757\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1094617,"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\/1094609"}],"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=1094609"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094609\/revisions"}],"predecessor-version":[{"id":1094620,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094609\/revisions\/1094620"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094617"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}