{"id":1129286,"date":"2025-01-08T20:28:15","date_gmt":"2025-01-08T12:28:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1129286.html"},"modified":"2025-01-08T20:28:17","modified_gmt":"2025-01-08T12:28:17","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e6%8c%87%e5%ae%9a%e8%b7%af%e5%be%84%e7%9a%84%e6%96%87%e4%bb%b6%e5%a4%b9%e5%90%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1129286.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u5939\u540d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095737\/fe6cb7ba-9b3d-4d2f-9d82-85412625b34a.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u5939\u540d\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u5939\u540d\u7684\u6b65\u9aa4\u975e\u5e38\u7b80\u5355\u548c\u76f4\u63a5\uff1a\u4f7f\u7528os\u6a21\u5757\u83b7\u53d6\u6587\u4ef6\u5939\u540d\u3001\u4f7f\u7528os.walk\u904d\u5386\u76ee\u5f55\u6811\u3001\u7ed3\u5408pathlib\u6a21\u5757\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\u3002<\/strong>\u5176\u4e2d\uff0cos\u6a21\u5757\u548cpathlib\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u529f\u80fd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u5c55\u5f00\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u5939\u540d\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757\u83b7\u53d6\u6587\u4ef6\u5939\u540d<\/h2>\n<\/p>\n<p><p>Python\u7684<code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7528\u4e8e\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u529f\u80fd\u3002\u8981\u8bfb\u53d6\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u5939\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.listdir()<\/code>\u51fd\u6570\u548c<code>os.path<\/code>\u6a21\u5757\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>os.listdir()<\/code>\u51fd\u6570\u8fd4\u56de\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6761\u76ee\uff0c\u5305\u62ec\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>os.path.isdir()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u6761\u76ee\u662f\u5426\u662f\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_folder_names(path):<\/p>\n<p>    try:<\/p>\n<p>        folder_names = [name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))]<\/p>\n<p>        return folder_names<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return &quot;The specified path does not exist.&quot;<\/p>\n<p>    except PermissionError:<\/p>\n<p>        return &quot;Permission denied to access the specified path.&quot;<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>path = &#39;\/your\/specified\/path&#39;<\/p>\n<p>print(get_folder_names(path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8def\u5f84\u53ef\u80fd\u4e0d\u5b58\u5728\u6216\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>&#8211;<code>except<\/code>\u5757\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_folder_names(path):<\/p>\n<p>    try:<\/p>\n<p>        folder_names = [name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))]<\/p>\n<p>        return folder_names<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return &quot;The specified path does not exist.&quot;<\/p>\n<p>    except PermissionError:<\/p>\n<p>        return &quot;Permission denied to access the specified path.&quot;<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>path = &#39;\/your\/specified\/path&#39;<\/p>\n<p>print(get_folder_names(path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528os.walk\u904d\u5386\u76ee\u5f55\u6811<\/h2>\n<\/p>\n<p><p><code>os.walk()<\/code>\u751f\u6210\u76ee\u5f55\u6811\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u540d\uff0c\u5b83\u662f\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u8fd4\u56de\u4e00\u4e2a\u4e09\u5143\u7ec4<code>(dirpath, dirnames, filenames)<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u904d\u5386\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_all_folder_names(root_path):<\/p>\n<p>    folder_names = []<\/p>\n<p>    for dirpath, dirnames, filenames in os.walk(root_path):<\/p>\n<p>        for dirname in dirnames:<\/p>\n<p>            folder_names.append(os.path.join(dirpath, dirname))<\/p>\n<p>    return folder_names<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>root_path = &#39;\/your\/specified\/path&#39;<\/p>\n<p>print(get_all_folder_names(root_path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u63a7\u5236\u904d\u5386\u6df1\u5ea6<\/h3>\n<\/p>\n<p><p><code>os.walk()<\/code>\u9ed8\u8ba4\u4f1a\u9012\u5f52\u904d\u5386\u6240\u6709\u5b50\u76ee\u5f55\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u63a7\u5236\u9012\u5f52\u6df1\u5ea6\u6765\u9650\u5236\u5176\u904d\u5386\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_folder_names_limited_depth(root_path, max_depth):<\/p>\n<p>    folder_names = []<\/p>\n<p>    for dirpath, dirnames, filenames in os.walk(root_path):<\/p>\n<p>        depth = dirpath[len(root_path):].count(os.sep)<\/p>\n<p>        if depth &lt; max_depth:<\/p>\n<p>            for dirname in dirnames:<\/p>\n<p>                folder_names.append(os.path.join(dirpath, dirname))<\/p>\n<p>        else:<\/p>\n<p>            del dirnames[:]  # \u9632\u6b62\u8fdb\u4e00\u6b65\u9012\u5f52<\/p>\n<p>    return folder_names<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>root_path = &#39;\/your\/specified\/path&#39;<\/p>\n<p>max_depth = 2<\/p>\n<p>print(get_folder_names_limited_depth(root_path, max_depth))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u7ed3\u5408pathlib\u6a21\u5757\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5b83\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u76f4\u89c2\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def get_folder_names_pathlib(path):<\/p>\n<p>    try:<\/p>\n<p>        p = Path(path)<\/p>\n<p>        folder_names = [x.name for x in p.iterdir() if x.is_dir()]<\/p>\n<p>        return folder_names<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return &quot;The specified path does not exist.&quot;<\/p>\n<p>    except PermissionError:<\/p>\n<p>        return &quot;Permission denied to access the specified path.&quot;<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>path = &#39;\/your\/specified\/path&#39;<\/p>\n<p>print(get_folder_names_pathlib(path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u9012\u5f52\u904d\u5386\u76ee\u5f55<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u7684<code>rglob()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u9012\u5f52\u904d\u5386\u76ee\u5f55\u6811\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def get_all_folder_names_pathlib(root_path):<\/p>\n<p>    p = Path(root_path)<\/p>\n<p>    folder_names = [x for x in p.rglob(&#39;*&#39;) if x.is_dir()]<\/p>\n<p>    return folder_names<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>root_path = &#39;\/your\/specified\/path&#39;<\/p>\n<p>print(get_all_folder_names_pathlib(root_path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u63a7\u5236\u904d\u5386\u6df1\u5ea6<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c\u6761\u4ef6\u5224\u65ad\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u9012\u5f52\u7684\u6df1\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def get_folder_names_limited_depth_pathlib(root_path, max_depth):<\/p>\n<p>    p = Path(root_path)<\/p>\n<p>    folder_names = [x for x in p.rglob(&#39;*&#39;) if x.is_dir() and len(x.relative_to(root_path).parts) &lt;= max_depth]<\/p>\n<p>    return folder_names<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>root_path = &#39;\/your\/specified\/path&#39;<\/p>\n<p>max_depth = 2<\/p>\n<p>print(get_folder_names_limited_depth_pathlib(root_path, max_depth))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8bfb\u53d6\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u5939\u540d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528os\u6a21\u5757\u83b7\u53d6\u6587\u4ef6\u5939\u540d<\/strong>\uff1a\u9002\u5408\u4e8e\u7b80\u5355\u7684\u76ee\u5f55\u8bfb\u53d6\u4efb\u52a1\uff0c\u4ee3\u7801\u7b80\u6d01\u660e\u4e86\u3002<\/li>\n<li><strong>\u4f7f\u7528os.walk\u904d\u5386\u76ee\u5f55\u6811<\/strong>\uff1a\u9002\u5408\u4e8e\u9700\u8981\u904d\u5386\u6574\u4e2a\u76ee\u5f55\u6811\u6216\u63a7\u5236\u904d\u5386\u6df1\u5ea6\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u7ed3\u5408pathlib\u6a21\u5757\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c<\/strong>\uff1a\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4f7f\u4ee3\u7801\u66f4\u6613\u8bfb\u6613\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u76ee\u5f55\u8bfb\u53d6\u4efb\u52a1\uff0c\u8fd8\u662f\u590d\u6742\u7684\u76ee\u5f55\u6811\u904d\u5386\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u7279\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u5939\u540d\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528Python\u7684<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>listdir()<\/code>\u51fd\u6570\u6765\u5217\u51fa\u6307\u5b9a\u8def\u5f84\u4e2d\u7684\u6240\u6709\u9879\u76ee\uff0c\u5e76\u901a\u8fc7<code>os.path.isdir()<\/code>\u51fd\u6570\u6765\u7b5b\u9009\u51fa\u6587\u4ef6\u5939\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\npath = &#39;\u4f60\u7684\u8def\u5f84&#39;\nfolders = [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]\nprint(folders)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd4\u56de\u8def\u5f84\u4e0b\u6240\u6709\u7684\u6587\u4ef6\u5939\u540d\u79f0\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u5939\u7684\u5b8c\u6574\u8def\u5f84\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.path.join()<\/code>\u51fd\u6570\u5c06\u6587\u4ef6\u5939\u540d\u79f0\u4e0e\u6307\u5b9a\u8def\u5f84\u7ed3\u5408\uff0c\u4ece\u800c\u83b7\u5f97\u5b8c\u6574\u7684\u6587\u4ef6\u5939\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\npath = &#39;\u4f60\u7684\u8def\u5f84&#39;\nfolders = [os.path.join(path, f) for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]\nprint(folders)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u8f93\u51fa\u5b8c\u6574\u7684\u6587\u4ef6\u5939\u8def\u5f84\u5217\u8868\uff0c\u4fbf\u4e8e\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u8bfb\u53d6\u6587\u4ef6\u5939\u540d\u65f6\u8fc7\u6ee4\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u5728\u5217\u51fa\u6587\u4ef6\u5939\u540d\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u6761\u4ef6\u6765\u8fc7\u6ee4\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u5939\u3002\u4f8b\u5982\uff0c\u5982\u679c\u53ea\u60f3\u83b7\u53d6\u4ee5\u7279\u5b9a\u524d\u7f00\u5f00\u5934\u7684\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u5728\u5217\u8868\u63a8\u5bfc\u4e2d\u52a0\u5165\u6761\u4ef6\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\npath = &#39;\u4f60\u7684\u8def\u5f84&#39;\nprefix = &#39;\u524d\u7f00&#39;\nfolders = [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f)) and f.startswith(prefix)]\nprint(folders)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u80fd\u5f97\u5230\u7b26\u5408\u6761\u4ef6\u7684\u6587\u4ef6\u5939\u540d\u79f0\uff0c\u5e2e\u52a9\u60a8\u66f4\u52a0\u7cbe\u51c6\u5730\u7ba1\u7406\u6587\u4ef6\u5939\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u5939\u540d\u7684\u6b65\u9aa4\u975e\u5e38\u7b80\u5355\u548c\u76f4\u63a5\uff1a\u4f7f\u7528os\u6a21\u5757\u83b7\u53d6\u6587\u4ef6\u5939\u540d\u3001\u4f7f\u7528os.walk\u904d\u5386\u76ee\u5f55\u6811 [&hellip;]","protected":false},"author":3,"featured_media":1129296,"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\/1129286"}],"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=1129286"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129286\/revisions"}],"predecessor-version":[{"id":1129299,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129286\/revisions\/1129299"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1129296"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1129286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1129286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1129286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}