{"id":1134437,"date":"2025-01-08T21:15:40","date_gmt":"2025-01-08T13:15:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1134437.html"},"modified":"2025-01-08T21:15:44","modified_gmt":"2025-01-08T13:15:44","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e6%96%87%e4%bb%b6%e5%a4%b9%e4%b8%8b%e7%9a%84%e6%96%87%e4%bb%b6%e5%90%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1134437.html","title":{"rendered":"python\u5982\u4f55\u6253\u5370\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\u540d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103356\/bbc9f3e4-e06e-4c74-8397-e9a4527187dd.webp\" alt=\"python\u5982\u4f55\u6253\u5370\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\u540d\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u6253\u5370\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528os\u5e93\u4e2d\u7684listdir\u51fd\u6570\u3001os.walk\u51fd\u6570\u4ee5\u53capathlib\u5e93\u7684Path\u7c7b\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528os.listdir\u3001\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528os.walk\u3001\u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528pathlib.Path.iterdir<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528os.listdir<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>os.listdir\u662fos\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u8fd4\u56de\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u540d\u3002\u5b83\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u4f46\u4e0d\u63d0\u4f9b\u9012\u5f52\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def list_files(directory):<\/p>\n<p>    try:<\/p>\n<p>        files = os.listdir(directory)<\/p>\n<p>        for file in files:<\/p>\n<p>            print(file)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(&quot;The specified directory does not exist.&quot;)<\/p>\n<p>    except PermissionError:<\/p>\n<p>        print(&quot;Permission denied to access the directory.&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>list_files(&#39;\/path\/to\/directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li>\u4ee3\u7801\u7b80\u6d01\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u652f\u6301\u9012\u5f52\u5217\u51fa\u5b50\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u3002<\/li>\n<li>\u5bf9\u5f02\u5e38\u5904\u7406\u7684\u8981\u6c42\u66f4\u9ad8\uff0c\u6bd4\u5982\u76ee\u5f55\u4e0d\u5b58\u5728\u6216\u6743\u9650\u4e0d\u8db3\u3002<\/li>\n<\/ul>\n<p><h4>3. \u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528os.listdir\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u4f8b\u5982FileNotFoundError\u548cPermissionError\u3002\u8be5\u65b9\u6cd5\u9002\u5408\u7528\u4e8e\u5217\u51fa\u5355\u5c42\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u540d\uff0c\u4f46\u5982\u679c\u9700\u8981\u9012\u5f52\u5730\u5217\u51fa\u6240\u6709\u5b50\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u540d\uff0c\u5219\u4e0d\u592a\u9002\u7528\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e8c\u3001\u4f7f\u7528os.walk<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>os.walk\u662f\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u7528\u4e8e\u9012\u5f52\u904d\u5386\u76ee\u5f55\u6811\u3002\u5b83\u8fd4\u56de\u4e09\u5143\u7ec4(root, dirs, files)\uff0c\u5176\u4e2droot\u662f\u5f53\u524d\u76ee\u5f55\u8def\u5f84\uff0cdirs\u662f\u5b50\u76ee\u5f55\u5217\u8868\uff0cfiles\u662f\u6587\u4ef6\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def list_files_recursive(directory):<\/p>\n<p>    try:<\/p>\n<p>        for root, dirs, files in os.walk(directory):<\/p>\n<p>            for file in files:<\/p>\n<p>                print(os.path.join(root, file))<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(&quot;The specified directory does not exist.&quot;)<\/p>\n<p>    except PermissionError:<\/p>\n<p>        print(&quot;Permission denied to access the directory.&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>list_files_recursive(&#39;\/path\/to\/directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u652f\u6301\u9012\u5f52\u904d\u5386\uff0c\u9002\u5408\u590d\u6742\u7684\u76ee\u5f55\u7ed3\u6784\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u5f53\u524d\u76ee\u5f55\u3001\u5b50\u76ee\u5f55\u548c\u6587\u4ef6\u7684\u5206\u79bb\u4fe1\u606f\uff0c\u65b9\u4fbf\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u751f\u6210\u7684\u4ee3\u7801\u53ef\u80fd\u76f8\u5bf9\u590d\u6742\u3002<\/li>\n<li>\u5728\u5904\u7406\u5927\u76ee\u5f55\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982\u5176\u4ed6\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><h4>3. \u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>os.walk\u975e\u5e38\u9002\u5408\u7528\u4e8e\u9700\u8981\u9012\u5f52\u904d\u5386\u76ee\u5f55\u7684\u573a\u666f\u3002\u5b83\u4e0d\u4ec5\u5217\u51fa\u5f53\u524d\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\uff0c\u8fd8\u4f1a\u9012\u5f52\u5730\u5217\u51fa\u6240\u6709\u5b50\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u3002\u4f7f\u7528os.walk\u65f6\uff0c\u540c\u6837\u9700\u8981\u6ce8\u610f\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e09\u3001\u4f7f\u7528pathlib.Path.iterdir<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>pathlib\u662fPython 3.4\u5f15\u5165\u7684\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u3002Path.iterdir\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u7528\u4e8e\u8fed\u4ee3\u76ee\u5f55\u4e2d\u7684\u6761\u76ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def list_files_pathlib(directory):<\/p>\n<p>    try:<\/p>\n<p>        p = Path(directory)<\/p>\n<p>        for file in p.iterdir():<\/p>\n<p>            if file.is_file():<\/p>\n<p>                print(file)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(&quot;The specified directory does not exist.&quot;)<\/p>\n<p>    except PermissionError:<\/p>\n<p>        print(&quot;Permission denied to access the directory.&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>list_files_pathlib(&#39;\/path\/to\/directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9762\u5411\u5bf9\u8c61\uff0c\u66f4\u52a0\u73b0\u4ee3\u548cPythonic\u3002<\/li>\n<li>\u4ee3\u7801\u66f4\u6613\u8bfb\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ec5\u652f\u6301Python 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/li>\n<li>\u9700\u8981\u719f\u6089pathlib\u6a21\u5757\u7684\u7528\u6cd5\u3002<\/li>\n<\/ul>\n<p><h4>3. \u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>pathlib.Path.iterdir\u65b9\u6cd5\u9002\u5408\u73b0\u4ee3Python\u4ee3\u7801\u98ce\u683c\uff0c\u63d0\u4f9b\u4e86\u66f4\u6613\u8bfb\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u76ee\u5f55\u904d\u5386\u4efb\u52a1\uff0cpathlib\u7684\u4ee3\u7801\u66f4\u7b80\u6d01\u4e14\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6253\u5370\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\u540d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\u3002<strong>os.listdir<\/strong>\u9002\u5408\u7b80\u5355\u7684\u76ee\u5f55\u904d\u5386\u4efb\u52a1\uff0c<strong>os.walk<\/strong>\u9002\u5408\u9012\u5f52\u904d\u5386\u590d\u6742\u76ee\u5f55\uff0c<strong>pathlib.Path.iterdir<\/strong>\u5219\u63d0\u4f9b\u4e86\u73b0\u4ee3\u7684\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u63a8\u8350\u4f7f\u7528os.walk\u8fdb\u884c\u590d\u6742\u76ee\u5f55\u7ed3\u6784\u7684\u904d\u5386\uff0cpathlib.Path.iterdir\u9002\u7528\u4e8e\u73b0\u4ee3Python\u4ee3\u7801\u98ce\u683c\u7684\u7b80\u5355\u76ee\u5f55\u904d\u5386\u4efb\u52a1\uff0c\u800cos.listdir\u9002\u5408\u521d\u5b66\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u624d\u80fd\u8fbe\u5230\u6700\u4f73\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5217\u51fa\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u540d\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6216<code>pathlib<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u540d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_path = &#39;\u60a8\u7684\u6587\u4ef6\u5939\u8def\u5f84&#39;\nfor filename in os.listdir(folder_path):\n    if os.path.isfile(os.path.join(folder_path, filename)):\n        print(filename)\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">from pathlib import Path\n\nfolder_path = Path(&#39;\u60a8\u7684\u6587\u4ef6\u5939\u8def\u5f84&#39;)\nfor file in folder_path.iterdir():\n    if file.is_file():\n        print(file.name)\n<\/code><\/pre>\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u6253\u5370\u51fa\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u8fc7\u6ee4\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u540d\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u60a8\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u6dfb\u52a0\u6761\u4ef6\u6765\u8fc7\u6ee4\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u53ea\u60f3\u5217\u51fa<code>.txt<\/code>\u6587\u4ef6\uff0c\u53ef\u4ee5\u8fd9\u6837\u4fee\u6539\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">for filename in os.listdir(folder_path):\n    if filename.endswith(&#39;.txt&#39;):\n        print(filename)\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>pathlib<\/code>\u65f6\u4e5f\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">for file in folder_path.glob(&#39;*.txt&#39;):\n    print(file.name)\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u4f7f\u5f97\u60a8\u80fd\u591f\u8f7b\u677e\u7ba1\u7406\u548c\u7b5b\u9009\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728\u6253\u5370\u6587\u4ef6\u540d\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u5939\u4e2d\u7684\u5b50\u76ee\u5f55\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u540c\u65f6\u5217\u51fa\u6240\u6709\u5b50\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def list_files(directory):\n    for filename in os.listdir(directory):\n        full_path = os.path.join(directory, filename)\n        if os.path.isdir(full_path):\n            list_files(full_path)  # \u9012\u5f52\u8c03\u7528\n        else:\n            print(filename)\n\nlist_files(&#39;\u60a8\u7684\u6587\u4ef6\u5939\u8def\u5f84&#39;)\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u7684\u9012\u5f52\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff1a  <\/p>\n<pre><code class=\"language-python\">for file in folder_path.rglob(&#39;*&#39;):\n    if file.is_file():\n        print(file.name)\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u83b7\u53d6\u6574\u4e2a\u76ee\u5f55\u6811\u4e2d\u7684\u6587\u4ef6\u540d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u6253\u5370\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528os\u5e93\u4e2d\u7684listdir\u51fd\u6570\u3001os.walk\u51fd\u6570\u4ee5\u53capathl [&hellip;]","protected":false},"author":3,"featured_media":1134448,"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\/1134437"}],"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=1134437"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134437\/revisions"}],"predecessor-version":[{"id":1134450,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134437\/revisions\/1134450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1134448"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1134437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1134437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1134437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}