{"id":1061253,"date":"2024-12-31T15:41:14","date_gmt":"2024-12-31T07:41:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1061253.html"},"modified":"2024-12-31T15:41:16","modified_gmt":"2024-12-31T07:41:16","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e6%96%87%e4%bb%b6%e5%a4%b9%e4%b8%8b%e9%9d%a2%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1061253.html","title":{"rendered":"Python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u5939\u4e0b\u9762\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/28a573bf-2ae1-45f5-bf5a-241141d18c49.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u5939\u4e0b\u9762\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u6587\u4ef6\u5939\u5185\u5bb9\u7684\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528glob\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528os\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6\u5939\u5185\u5bb9\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>Python \u662f\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u64cd\u4f5c\u3002\u4e3a\u4e86\u8bfb\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u5185\u5bb9\uff0cPython \u63d0\u4f9b\u4e86\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684 os \u6a21\u5757\u3001glob \u6a21\u5757\u548c pathlib \u6a21\u5757\u3002<strong>\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528glob\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5e76\u4e14\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\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\uff0c\u5305\u62ec\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u3002\u4f7f\u7528os\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6\u5939\u5185\u5bb9\u7684\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u5373\u53ef\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u8981\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528os.listdir()\u51fd\u6570\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &#39;your_folder_path&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>items = os.listdir(folder_path)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>for item in items:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165os\u6a21\u5757\uff0c\u7136\u540e\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84\uff0c\u5e76\u4f7f\u7528os.listdir()\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u3002\u6700\u540e\uff0c\u6211\u4eec\u904d\u5386\u5e76\u6253\u5370\u8fd9\u4e9b\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u533a\u5206\u6587\u4ef6\u548c\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>os.listdir()\u51fd\u6570\u8fd4\u56de\u7684\u5217\u8868\u4e2d\u5305\u542b\u4e86\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684\u540d\u79f0\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u533a\u5206\u5b83\u4eec\uff0c\u53ef\u4ee5\u4f7f\u7528os.path.isfile()\u548cos.path.isdir()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &#39;your_folder_path&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>items = os.listdir(folder_path)<\/p>\n<h2><strong>\u904d\u5386\u5e76\u533a\u5206\u6587\u4ef6\u548c\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>for item in items:<\/p>\n<p>    item_path = os.path.join(folder_path, item)<\/p>\n<p>    if os.path.isfile(item_path):<\/p>\n<p>        print(f&#39;File: {item}&#39;)<\/p>\n<p>    elif os.path.isdir(item_path):<\/p>\n<p>        print(f&#39;Folder: {item}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.path.join()\u51fd\u6570\u5c06\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u540d\u79f0\u4e0e\u6587\u4ef6\u5939\u8def\u5f84\u7ec4\u5408\u6210\u5b8c\u6574\u7684\u8def\u5f84\uff0c\u7136\u540e\u4f7f\u7528os.path.isfile()\u548cos.path.isdir()\u51fd\u6570\u5206\u522b\u5224\u65ad\u8be5\u8def\u5f84\u662f\u6587\u4ef6\u8fd8\u662f\u6587\u4ef6\u5939\uff0c\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528glob\u6a21\u5757<\/h3>\n<\/p>\n<p><p>glob\u6a21\u5757\u662f\u4e00\u4e2a\u6587\u4ef6\u540d\u6a21\u5f0f\u5339\u914d\u5de5\u5177\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u5f0f\u5339\u914d\u6765\u67e5\u627e\u7b26\u5408\u6761\u4ef6\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002glob\u6a21\u5757\u975e\u5e38\u9002\u5408\u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u4f8b\u5982\u6240\u6709\u7684txt\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u8981\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528glob.glob()\u51fd\u6570\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u7b26\u5408\u6307\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684\u8def\u5f84\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &#39;your_folder_path&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>items = glob.glob(f&#39;{folder_path}\/*&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>for item in items:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165glob\u6a21\u5757\uff0c\u7136\u540e\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84\uff0c\u5e76\u4f7f\u7528glob.glob()\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u3002\u6700\u540e\uff0c\u6211\u4eec\u904d\u5386\u5e76\u6253\u5370\u8fd9\u4e9b\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u83b7\u53d6\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u53ea\u9700\u8981\u83b7\u53d6\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u4f8b\u5982\u6240\u6709\u7684txt\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728glob.glob()\u51fd\u6570\u4e2d\u4f7f\u7528\u901a\u914d\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &#39;your_folder_path&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709txt\u6587\u4ef6<\/strong><\/h2>\n<p>txt_files = glob.glob(f&#39;{folder_path}\/*.txt&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>for txt_file in txt_files:<\/p>\n<p>    print(txt_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u901a\u914d\u7b26*.txt\u5339\u914d\u6240\u6709\u7684txt\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528glob.glob()\u51fd\u6570\u83b7\u53d6\u7b26\u5408\u6761\u4ef6\u7684\u6587\u4ef6\u8def\u5f84\u5217\u8868\u3002\u6700\u540e\uff0c\u6211\u4eec\u904d\u5386\u5e76\u6253\u5370\u8fd9\u4e9btxt\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u65b0\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u3002\u76f8\u6bd4\u4e8eos\u6a21\u5757\u548cglob\u6a21\u5757\uff0cpathlib\u6a21\u5757\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u8981\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528pathlib.Path\u5bf9\u8c61\u7684iterdir()\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u751f\u6210\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u7684Path\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = Path(&#39;your_folder_path&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>items = folder_path.iterdir()<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>for item in items:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165Path\u7c7b\uff0c\u7136\u540e\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84\uff0c\u5e76\u4f7f\u7528iterdir()\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u3002\u6700\u540e\uff0c\u6211\u4eec\u904d\u5386\u5e76\u6253\u5370\u8fd9\u4e9b\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u7684Path\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u533a\u5206\u6587\u4ef6\u548c\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>Path\u5bf9\u8c61\u63d0\u4f9b\u4e86is_file()\u548cis_dir()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u8def\u5f84\u662f\u6587\u4ef6\u8fd8\u662f\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = Path(&#39;your_folder_path&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>items = folder_path.iterdir()<\/p>\n<h2><strong>\u904d\u5386\u5e76\u533a\u5206\u6587\u4ef6\u548c\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>for item in items:<\/p>\n<p>    if item.is_file():<\/p>\n<p>        print(f&#39;File: {item.name}&#39;)<\/p>\n<p>    elif item.is_dir():<\/p>\n<p>        print(f&#39;Folder: {item.name}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528is_file()\u548cis_dir()\u65b9\u6cd5\u5206\u522b\u5224\u65adPath\u5bf9\u8c61\u662f\u6587\u4ef6\u8fd8\u662f\u6587\u4ef6\u5939\uff0c\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9012\u5f52\u83b7\u53d6\u6587\u4ef6\u5939\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u9012\u5f52\u5730\u83b7\u53d6\u6587\u4ef6\u5939\u53ca\u5176\u5b50\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u7ed3\u5408\u9012\u5f52\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528os\u6a21\u5757\u9012\u5f52\u83b7\u53d6\u6587\u4ef6\u5939\u5185\u5bb9<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def list_files_and_folders(folder_path):<\/p>\n<p>    items = os.listdir(folder_path)<\/p>\n<p>    for item in items:<\/p>\n<p>        item_path = os.path.join(folder_path, item)<\/p>\n<p>        if os.path.isfile(item_path):<\/p>\n<p>            print(f&#39;File: {item_path}&#39;)<\/p>\n<p>        elif os.path.isdir(item_path):<\/p>\n<p>            print(f&#39;Folder: {item_path}&#39;)<\/p>\n<p>            list_files_and_folders(item_path)<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &#39;your_folder_path&#39;<\/p>\n<h2><strong>\u9012\u5f52\u83b7\u53d6\u6587\u4ef6\u5939\u5185\u5bb9<\/strong><\/h2>\n<p>list_files_and_folders(folder_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570list_files_and_folders()\uff0c\u7528\u4e8e\u9012\u5f52\u83b7\u53d6\u6587\u4ef6\u5939\u53ca\u5176\u5b50\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u51fd\u6570\u5185\u90e8\u4f7f\u7528os.listdir()\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\uff0c\u5e76\u4f7f\u7528os.path.isfile()\u548cos.path.isdir()\u5224\u65ad\u662f\u6587\u4ef6\u8fd8\u662f\u6587\u4ef6\u5939\u3002\u5982\u679c\u662f\u6587\u4ef6\uff0c\u5219\u76f4\u63a5\u8f93\u51fa\uff1b\u5982\u679c\u662f\u6587\u4ef6\u5939\uff0c\u5219\u9012\u5f52\u8c03\u7528list_files_and_folders()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528pathlib\u6a21\u5757\u9012\u5f52\u83b7\u53d6\u6587\u4ef6\u5939\u5185\u5bb9<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def list_files_and_folders(folder_path):<\/p>\n<p>    items = folder_path.iterdir()<\/p>\n<p>    for item in items:<\/p>\n<p>        if item.is_file():<\/p>\n<p>            print(f&#39;File: {item}&#39;)<\/p>\n<p>        elif item.is_dir():<\/p>\n<p>            print(f&#39;Folder: {item}&#39;)<\/p>\n<p>            list_files_and_folders(item)<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = Path(&#39;your_folder_path&#39;)<\/p>\n<h2><strong>\u9012\u5f52\u83b7\u53d6\u6587\u4ef6\u5939\u5185\u5bb9<\/strong><\/h2>\n<p>list_files_and_folders(folder_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570list_files_and_folders()\uff0c\u7528\u4e8e\u9012\u5f52\u83b7\u53d6\u6587\u4ef6\u5939\u53ca\u5176\u5b50\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u51fd\u6570\u5185\u90e8\u4f7f\u7528iterdir()\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\uff0c\u5e76\u4f7f\u7528is_file()\u548cis_dir()\u5224\u65ad\u662f\u6587\u4ef6\u8fd8\u662f\u6587\u4ef6\u5939\u3002\u5982\u679c\u662f\u6587\u4ef6\uff0c\u5219\u76f4\u63a5\u8f93\u51fa\uff1b\u5982\u679c\u662f\u6587\u4ef6\u5939\uff0c\u5219\u9012\u5f52\u8c03\u7528list_files_and_folders()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u5185\u5bb9\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001glob\u6a21\u5757\u548cpathlib\u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002os\u6a21\u5757\u548cglob\u6a21\u5757\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u64cd\u4f5c\uff0c\u800cpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u7528\u7684\u9762\u5411\u5bf9\u8c61\u63a5\u53e3\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u6765\u83b7\u53d6\u6587\u4ef6\u5939\u53ca\u5176\u5b50\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u8bfb\u53d6\u6587\u4ef6\u5939\u5185\u5bb9\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6\u7279\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u540d\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>os<\/code>\u6a21\u5757\u6765\u8bfb\u53d6\u7279\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u540d\u3002\u901a\u8fc7<code>os.listdir()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u83b7\u53d6\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u7684\u540d\u79f0\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_path = &#39;\u60a8\u7684\u6587\u4ef6\u5939\u8def\u5f84&#39;\nfiles = os.listdir(folder_path)\nfor file in files:\n    print(file)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6253\u5370\u51fa\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u5185\u5bb9\uff0c\u5305\u62ec\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u3002<\/p>\n<p><strong>\u5728\u8bfb\u53d6\u6587\u4ef6\u5939\u5185\u5bb9\u65f6\uff0c\u5982\u4f55\u8fc7\u6ee4\u51fa\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u53ea\u60f3\u8bfb\u53d6\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff08\u4f8b\u5982<code>.txt<\/code>\u6587\u4ef6\uff09\uff0c\u53ef\u4ee5\u7ed3\u5408<code>os.listdir()<\/code>\u548c\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u5b9e\u73b0\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;\ntxt_files = [file for file in os.listdir(folder_path) if file.endswith(&#39;.txt&#39;)]\nfor txt_file in txt_files:\n    print(txt_file)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u53ea\u8f93\u51fa\u4ee5<code>.txt<\/code>\u7ed3\u5c3e\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8bfb\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u5b50\u6587\u4ef6\u5939\u548c\u6587\u4ef6\uff1f<\/strong><br \/>\u4f7f\u7528<code>os.walk()<\/code>\u53ef\u4ee5\u904d\u5386\u6587\u4ef6\u5939\u53ca\u5176\u5b50\u6587\u4ef6\u5939\uff0c\u4ece\u800c\u8bfb\u53d6\u6240\u6709\u5185\u5bb9\u3002\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u63d0\u4f9b\u6587\u4ef6\u5939\u8def\u5f84\u3001\u5b50\u6587\u4ef6\u5939\u540d\u79f0\u548c\u6587\u4ef6\u540d\u79f0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\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 root, dirs, files in os.walk(folder_path):\n    for file in files:\n        print(os.path.join(root, file))\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u83b7\u53d6\u6307\u5b9a\u6587\u4ef6\u5939\u53ca\u5176\u6240\u6709\u5b50\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u6587\u4ef6\u5939\u5185\u5bb9\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528glob\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528o [&hellip;]","protected":false},"author":3,"featured_media":1061268,"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\/1061253"}],"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=1061253"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061253\/revisions"}],"predecessor-version":[{"id":1061272,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061253\/revisions\/1061272"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1061268"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1061253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1061253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1061253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}