{"id":1109828,"date":"2025-01-08T17:13:57","date_gmt":"2025-01-08T09:13:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109828.html"},"modified":"2025-01-08T17:14:00","modified_gmt":"2025-01-08T09:14:00","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%98%af%e4%b8%8d%e6%98%af%e7%a9%ba%e6%96%87%e4%bb%b6%e5%a4%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1109828.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u662f\u4e0d\u662f\u7a7a\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\/25072930\/2f3e9a8f-4be8-469a-bf53-c84a226d4891.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u662f\u4e0d\u662f\u7a7a\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7os\u6a21\u5757\u7684\u591a\u79cd\u65b9\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u6587\u4ef6\u5939\u662f\u5426\u4e3a\u7a7a\uff0c\u4f8b\u5982\u4f7f\u7528os.listdir()\u3001os.scandir()\u3001os.walk()\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528os.listdir()\u51fd\u6570\u6765\u5217\u51fa\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\uff0c\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u6587\u4ef6\u5939\u662f\u7a7a\u7684\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os.listdir()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>os.listdir()\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684\u540d\u79f0\u5217\u8868\u3002\u901a\u8fc7\u68c0\u67e5\u8be5\u5217\u8868\u662f\u5426\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u5224\u65ad\u6587\u4ef6\u5939\u662f\u5426\u4e3a\u7a7a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def is_empty_folder(path):<\/p>\n<p>    if os.path.exists(path) and os.path.isdir(path):<\/p>\n<p>        return len(os.listdir(path)) == 0<\/p>\n<p>    return False<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>folder_path = &#39;\/path\/to\/folder&#39;<\/p>\n<p>if is_empty_folder(folder_path):<\/p>\n<p>    print(f&quot;{folder_path} \u662f\u7a7a\u6587\u4ef6\u5939\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{folder_path} \u4e0d\u662f\u7a7a\u6587\u4ef6\u5939\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>is_empty_folder<\/code>\u51fd\u6570\u4f1a\u9996\u5148\u68c0\u67e5\u7ed9\u5b9a\u7684\u8def\u5f84\u662f\u5426\u5b58\u5728\u5e76\u4e14\u662f\u5426\u662f\u4e00\u4e2a\u6587\u4ef6\u5939\u3002\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de\u8be5\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u5217\u8868\u7684\u957f\u5ea6\u662f\u5426\u4e3a0\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528os.scandir()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>os.scandir()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u76ee\u5f55\u6761\u76ee\u3002\u901a\u8fc7\u68c0\u67e5\u662f\u5426\u5b58\u5728\u4efb\u4f55\u6761\u76ee\uff0c\u53ef\u4ee5\u5224\u65ad\u6587\u4ef6\u5939\u662f\u5426\u4e3a\u7a7a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def is_empty_folder(path):<\/p>\n<p>    if os.path.exists(path) and os.path.isdir(path):<\/p>\n<p>        with os.scandir(path) as entries:<\/p>\n<p>            for entry in entries:<\/p>\n<p>                return False<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>folder_path = &#39;\/path\/to\/folder&#39;<\/p>\n<p>if is_empty_folder(folder_path):<\/p>\n<p>    print(f&quot;{folder_path} \u662f\u7a7a\u6587\u4ef6\u5939\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{folder_path} \u4e0d\u662f\u7a7a\u6587\u4ef6\u5939\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>is_empty_folder<\/code>\u51fd\u6570\u4f7f\u7528<code>os.scandir()<\/code>\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6761\u76ee\u3002\u5982\u679c\u5b58\u5728\u4efb\u4f55\u6761\u76ee\uff0c\u5219\u8bf4\u660e\u6587\u4ef6\u5939\u4e0d\u662f\u7a7a\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528os.walk()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>os.walk()\u51fd\u6570\u751f\u6210\u76ee\u5f55\u6811\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u540d\u3002\u901a\u8fc7\u68c0\u67e5\u751f\u6210\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u5217\u8868\uff0c\u53ef\u4ee5\u5224\u65ad\u6587\u4ef6\u5939\u662f\u5426\u4e3a\u7a7a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def is_empty_folder(path):<\/p>\n<p>    if os.path.exists(path) and os.path.isdir(path):<\/p>\n<p>        for _, dirs, files in os.walk(path):<\/p>\n<p>            if dirs or files:<\/p>\n<p>                return False<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>folder_path = &#39;\/path\/to\/folder&#39;<\/p>\n<p>if is_empty_folder(folder_path):<\/p>\n<p>    print(f&quot;{folder_path} \u662f\u7a7a\u6587\u4ef6\u5939\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{folder_path} \u4e0d\u662f\u7a7a\u6587\u4ef6\u5939\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>is_empty_folder<\/code>\u51fd\u6570\u4f7f\u7528<code>os.walk()<\/code>\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u76ee\u5f55\u548c\u6587\u4ef6\u3002\u5982\u679c\u5b58\u5728\u4efb\u4f55\u76ee\u5f55\u6216\u6587\u4ef6\uff0c\u5219\u8bf4\u660e\u6587\u4ef6\u5939\u4e0d\u662f\u7a7a\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Pathlib\u5e93<\/h3>\n<\/p>\n<p><p>Pathlib\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u66f4\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u3002\u4f7f\u7528Pathlib\u5e93\u4e5f\u53ef\u4ee5\u8f7b\u677e\u5224\u65ad\u6587\u4ef6\u5939\u662f\u5426\u4e3a\u7a7a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def is_empty_folder(path):<\/p>\n<p>    folder = Path(path)<\/p>\n<p>    if folder.exists() and folder.is_dir():<\/p>\n<p>        return not any(folder.iterdir())<\/p>\n<p>    return False<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>folder_path = &#39;\/path\/to\/folder&#39;<\/p>\n<p>if is_empty_folder(folder_path):<\/p>\n<p>    print(f&quot;{folder_path} \u662f\u7a7a\u6587\u4ef6\u5939\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{folder_path} \u4e0d\u662f\u7a7a\u6587\u4ef6\u5939\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>is_empty_folder<\/code>\u51fd\u6570\u4f7f\u7528Pathlib\u5e93\u7684<code>Path<\/code>\u5bf9\u8c61\u6765\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u5e76\u4e14\u662f\u5426\u4e3a\u7a7a\u3002<code>Path.iterdir()<\/code>\u65b9\u6cd5\u8fd4\u56de\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6761\u76ee\uff0c\u5982\u679c\u6ca1\u6709\u6761\u76ee\uff0c\u5219\u8bf4\u660e\u6587\u4ef6\u5939\u662f\u7a7a\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5224\u65ad\u4e00\u4e2a\u6587\u4ef6\u5939\u662f\u5426\u4e3a\u7a7a\u7684\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os.listdir()\u3001os.scandir()\u3001os.walk()\u4ee5\u53caPathlib\u5e93\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5224\u65ad\u6587\u4ef6\u5939\u662f\u5426\u4e3a\u7a7a\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u5173\u952e\u662f\u8981\u786e\u4fdd\u9996\u5148\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u4ee5\u53ca\u662f\u5426\u662f\u4e00\u4e2a\u6709\u6548\u7684\u6587\u4ef6\u5939\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56e0\u8def\u5f84\u65e0\u6548\u6216\u8def\u5f84\u6307\u5411\u7684\u4e0d\u662f\u6587\u4ef6\u5939\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e00\u4e2a\u6587\u4ef6\u5939\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>listdir<\/code>\u51fd\u6570\u6765\u5224\u65ad\u6587\u4ef6\u5939\u662f\u5426\u4e3a\u7a7a\u3002\u5177\u4f53\u6b65\u9aa4\u662f\u5148\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>os.listdir(path)<\/code>\u5217\u51fa\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u3002\u5982\u679c\u8fd4\u56de\u7684\u5217\u8868\u4e3a\u7a7a\uff0c\u5219\u8be5\u6587\u4ef6\u5939\u4e3a\u7a7a\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\ndef is_empty_folder(folder_path):\n    return len(os.listdir(folder_path)) == 0\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5224\u65ad\u4e00\u4e2a\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>exists<\/code>\u51fd\u6570\u6765\u5224\u65ad\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002\u7ed3\u5408\u5224\u65ad\u6587\u4ef6\u5939\u662f\u5426\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u5f62\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u68c0\u67e5\u903b\u8f91\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\ndef check_folder(folder_path):\n    if os.path.exists(folder_path):\n        if len(os.listdir(folder_path)) == 0:\n            return &quot;\u6587\u4ef6\u5939\u5b58\u5728\u4f46\u4e3a\u7a7a&quot;\n        else:\n            return &quot;\u6587\u4ef6\u5939\u5b58\u5728\u4e14\u4e0d\u4e3a\u7a7a&quot;\n    else:\n        return &quot;\u6587\u4ef6\u5939\u4e0d\u5b58\u5728&quot;\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u5982\u4f55\u5220\u9664\u4e00\u4e2a\u7a7a\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u5728\u786e\u8ba4\u4e00\u4e2a\u6587\u4ef6\u5939\u662f\u7a7a\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.rmdir<\/code>\u51fd\u6570\u6765\u5220\u9664\u8be5\u6587\u4ef6\u5939\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>os.rmdir<\/code>\u53ea\u4f1a\u5220\u9664\u7a7a\u7684\u6587\u4ef6\u5939\u3002\u5982\u679c\u6587\u4ef6\u5939\u4e2d\u6709\u5185\u5bb9\uff0c\u5fc5\u987b\u5148\u6e05\u7a7a\u6587\u4ef6\u5939\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\ndef remove_empty_folder(folder_path):\n    if len(os.listdir(folder_path)) == 0:\n        os.rmdir(folder_path)\n        return &quot;\u7a7a\u6587\u4ef6\u5939\u5df2\u5220\u9664&quot;\n    else:\n        return &quot;\u6587\u4ef6\u5939\u4e0d\u4e3a\u7a7a\uff0c\u65e0\u6cd5\u5220\u9664&quot;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7os\u6a21\u5757\u7684\u591a\u79cd\u65b9\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u6587\u4ef6\u5939\u662f\u5426\u4e3a\u7a7a\uff0c\u4f8b\u5982\u4f7f\u7528os.listdir()\u3001os. [&hellip;]","protected":false},"author":3,"featured_media":1109841,"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\/1109828"}],"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=1109828"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109828\/revisions"}],"predecessor-version":[{"id":1109843,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109828\/revisions\/1109843"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1109841"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}