{"id":1158167,"date":"2025-01-13T18:37:18","date_gmt":"2025-01-13T10:37:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1158167.html"},"modified":"2025-01-13T18:37:21","modified_gmt":"2025-01-13T10:37:21","slug":"python%e5%a6%82%e4%bd%95%e6%89%be%e5%88%b0%e6%9c%ac%e5%9c%b0%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1158167.html","title":{"rendered":"python\u5982\u4f55\u627e\u5230\u672c\u5730\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25200212\/5b72ee2d-f529-4178-a9a5-a99ac7c5f859.webp\" alt=\"python\u5982\u4f55\u627e\u5230\u672c\u5730\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u627e\u5230\u672c\u5730\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c\u5305\u62ec<strong>\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528glob\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/strong>\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u4f46\u90fd\u80fd\u5e2e\u52a9\u6211\u4eec\u627e\u5230\u5e76\u64cd\u4f5c\u672c\u5730\u6587\u4ef6\u3002<strong>os\u6a21\u5757\u662f\u6700\u57fa\u7840\u7684\u65b9\u6cd5\uff0cglob\u6a21\u5757\u9002\u7528\u4e8e\u6a21\u5f0f\u5339\u914d\uff0cpathlib\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u548c\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001OS\u6a21\u5757<\/h3>\n<\/p>\n<p><p>OS\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\u3002\u4f7f\u7528os\u6a21\u5757\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u627e\u5230\u672c\u5730\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001os.listdir()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>os.listdir()\u65b9\u6cd5\u8fd4\u56de\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u8def\u5f84<\/strong><\/h2>\n<p>path = &quot;\/path\/to\/directory&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55<\/strong><\/h2>\n<p>files_and_dirs = os.listdir(path)<\/p>\n<h2><strong>\u6253\u5370\u6587\u4ef6\u548c\u76ee\u5f55<\/strong><\/h2>\n<p>for item in files_and_dirs:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001os.walk()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>os.walk()\u65b9\u6cd5\u751f\u6210\u76ee\u5f55\u6811\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u540d\uff0c\u5b83\u662f\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u9010\u5c42\u904d\u5386\u76ee\u5f55\u6811\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u8def\u5f84<\/strong><\/h2>\n<p>path = &quot;\/path\/to\/directory&quot;<\/p>\n<h2><strong>\u904d\u5386\u76ee\u5f55\u6811<\/strong><\/h2>\n<p>for root, dirs, files in os.walk(path):<\/p>\n<p>    print(&quot;\u5f53\u524d\u76ee\u5f55\u8def\u5f84:&quot;, root)<\/p>\n<p>    print(&quot;\u8be5\u76ee\u5f55\u4e0b\u7684\u5b50\u76ee\u5f55:&quot;, dirs)<\/p>\n<p>    print(&quot;\u8be5\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6:&quot;, files)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001Glob\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Glob\u6a21\u5757\u63d0\u4f9b\u4e86\u57fa\u4e8eUnix shell\u98ce\u683c\u7684\u8def\u5f84\u540d\u6a21\u5f0f\u6269\u5c55\u529f\u80fd\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u901a\u914d\u7b26\u6765\u67e5\u627e\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001glob.glob()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>glob.glob()\u65b9\u6cd5\u8fd4\u56de\u4e0e\u6307\u5b9a\u6a21\u5f0f\u5339\u914d\u7684\u6240\u6709\u6587\u4ef6\u8def\u5f84\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<h2><strong>\u6307\u5b9a\u8def\u5f84\u6a21\u5f0f<\/strong><\/h2>\n<p>pattern = &quot;\/path\/to\/directory\/*.txt&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u5339\u914d\u7684\u6587\u4ef6\u8def\u5f84\u5217\u8868<\/strong><\/h2>\n<p>files = glob.glob(pattern)<\/p>\n<h2><strong>\u6253\u5370\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>for file in files:<\/p>\n<p>    print(file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001glob.iglob()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>glob.iglob()\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u8fd9\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u65f6\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<h2><strong>\u6307\u5b9a\u8def\u5f84\u6a21\u5f0f<\/strong><\/h2>\n<p>pattern = &quot;\/path\/to\/directory\/*.txt&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u5339\u914d\u7684\u6587\u4ef6\u8def\u5f84\u8fed\u4ee3\u5668<\/strong><\/h2>\n<p>files = glob.iglob(pattern)<\/p>\n<h2><strong>\u6253\u5370\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>for file in files:<\/p>\n<p>    print(file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u548c\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001Path\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>Path\u5bf9\u8c61\u8868\u793a\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u65b9\u6cd5\u6765\u67e5\u627e\u548c\u64cd\u4f5c\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6307\u5b9a\u8def\u5f84<\/strong><\/h2>\n<p>path = Path(&quot;\/path\/to\/directory&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55<\/strong><\/h2>\n<p>for item in path.iterdir():<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001glob()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>Path\u5bf9\u8c61\u7684glob()\u65b9\u6cd5\u7c7b\u4f3c\u4e8eglob\u6a21\u5757\u7684glob()\u65b9\u6cd5\uff0c\u8fd4\u56de\u4e0e\u6307\u5b9a\u6a21\u5f0f\u5339\u914d\u7684\u6587\u4ef6\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6307\u5b9a\u8def\u5f84<\/strong><\/h2>\n<p>path = Path(&quot;\/path\/to\/directory&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u5339\u914d\u7684\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>files = path.glob(&quot;*.txt&quot;)<\/p>\n<h2><strong>\u6253\u5370\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>for file in files:<\/p>\n<p>    print(file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7efc\u5408\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u6765\u67e5\u627e\u548c\u64cd\u4f5c\u672c\u5730\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528os\u6a21\u5757\u548cglob\u6a21\u5757\u67e5\u627e\u6240\u6709\u6587\u672c\u6587\u4ef6\u5e76\u7edf\u8ba1\u5176\u884c\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import glob<\/p>\n<h2><strong>\u6307\u5b9a\u8def\u5f84<\/strong><\/h2>\n<p>path = &quot;\/path\/to\/directory&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u6587\u672c\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>pattern = os.path.join(path, &quot;*.txt&quot;)<\/p>\n<p>files = glob.glob(pattern)<\/p>\n<h2><strong>\u7edf\u8ba1\u6bcf\u4e2a\u6587\u4ef6\u7684\u884c\u6570<\/strong><\/h2>\n<p>for file in files:<\/p>\n<p>    with open(file, &#39;r&#39;) as f:<\/p>\n<p>        lines = f.readlines()<\/p>\n<p>        print(f&quot;{file} \u6709 {len(lines)} \u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6587\u4ef6\u67e5\u627e\u7684\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1\u3001\u6279\u91cf\u5904\u7406\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u6279\u91cf\u5904\u7406\u5927\u91cf\u6587\u4ef6\uff0c\u4f8b\u5982\u8bfb\u53d6\u591a\u4e2aCSV\u6587\u4ef6\u5e76\u8fdb\u884c\u6570\u636e\u6c47\u603b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u8bfb\u53d6\u591a\u4e2aCSV\u6587\u4ef6\u5e76\u5c06\u5176\u6570\u636e\u5408\u5e76\u5230\u4e00\u4e2aDataFrame\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import glob<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u6307\u5b9a\u8def\u5f84<\/strong><\/h2>\n<p>path = &quot;\/path\/to\/csv\/files&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709CSV\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>pattern = os.path.join(path, &quot;*.csv&quot;)<\/p>\n<p>files = glob.glob(pattern)<\/p>\n<h2><strong>\u8bfb\u53d6\u5e76\u5408\u5e76CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df_list = []<\/p>\n<p>for file in files:<\/p>\n<p>    df = pd.read_csv(file)<\/p>\n<p>    df_list.append(df)<\/p>\n<h2><strong>\u5408\u5e76\u6240\u6709DataFrame<\/strong><\/h2>\n<p>merged_df = pd.concat(df_list)<\/p>\n<h2><strong>\u6253\u5370\u5408\u5e76\u540e\u7684DataFrame<\/strong><\/h2>\n<p>print(merged_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6587\u4ef6\u5907\u4efd\u548c\u540c\u6b65<\/h4>\n<\/p>\n<p><p>\u5728\u8fd0\u7ef4\u548c\u7cfb\u7edf\u7ba1\u7406\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5b9a\u671f\u5907\u4efd\u548c\u540c\u6b65\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528os\u6a21\u5757\u548cshutil\u6a21\u5757\u5907\u4efd\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u5230\u53e6\u4e00\u4e2a\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<h2><strong>\u6307\u5b9a\u6e90\u76ee\u5f55\u548c\u76ee\u6807\u76ee\u5f55<\/strong><\/h2>\n<p>source_dir = &quot;\/path\/to\/source\/directory&quot;<\/p>\n<p>backup_dir = &quot;\/path\/to\/backup\/directory&quot;<\/p>\n<h2><strong>\u521b\u5efa\u76ee\u6807\u76ee\u5f55\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff09<\/strong><\/h2>\n<p>os.makedirs(backup_dir, exist_ok=True)<\/p>\n<h2><strong>\u904d\u5386\u6e90\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>for root, dirs, files in os.walk(source_dir):<\/p>\n<p>    for file in files:<\/p>\n<p>        # \u6784\u9020\u6e90\u6587\u4ef6\u8def\u5f84\u548c\u76ee\u6807\u6587\u4ef6\u8def\u5f84<\/p>\n<p>        source_file = os.path.join(root, file)<\/p>\n<p>        relative_path = os.path.relpath(root, source_dir)<\/p>\n<p>        backup_file = os.path.join(backup_dir, relative_path, file)<\/p>\n<p>        # \u521b\u5efa\u76ee\u6807\u76ee\u5f55\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff09<\/p>\n<p>        os.makedirs(os.path.dirname(backup_file), exist_ok=True)<\/p>\n<p>        # \u590d\u5236\u6587\u4ef6<\/p>\n<p>        shutil.copy2(source_file, backup_file)<\/p>\n<p>        print(f&quot;\u5907\u4efd\u6587\u4ef6\uff1a{source_file} \u5230 {backup_file}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u67e5\u627e\u548c\u64cd\u4f5c\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u56e0\u6b64\uff0c\u826f\u597d\u7684\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u67e5\u627e\u6587\u4ef6\u65f6\u8fdb\u884c\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import glob<\/p>\n<p>import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h2>\n<p>logging.basicConfig(filename=&#39;file_search.log&#39;, level=logging.ERROR,<\/p>\n<p>                    format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<h2><strong>\u6307\u5b9a\u8def\u5f84<\/strong><\/h2>\n<p>path = &quot;\/path\/to\/directory&quot;<\/p>\n<p>try:<\/p>\n<p>    # \u83b7\u53d6\u6240\u6709\u6587\u672c\u6587\u4ef6\u8def\u5f84<\/p>\n<p>    pattern = os.path.join(path, &quot;*.txt&quot;)<\/p>\n<p>    files = glob.glob(pattern)<\/p>\n<p>    # \u6253\u5370\u6587\u4ef6\u8def\u5f84<\/p>\n<p>    for file in files:<\/p>\n<p>        print(file)<\/p>\n<p>except Exception as e:<\/p>\n<p>    logging.error(f&quot;\u67e5\u627e\u6587\u4ef6\u65f6\u53d1\u751f\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6027\u80fd\u4f18\u5316\u6280\u5de7\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u53ef\u4ee5\u8282\u7701\u5185\u5b58\uff0c\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528glob.iglob()\u65b9\u6cd5\u800c\u4e0d\u662fglob.glob()\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<h2><strong>\u6307\u5b9a\u8def\u5f84\u6a21\u5f0f<\/strong><\/h2>\n<p>pattern = &quot;\/path\/to\/directory\/*.txt&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u5339\u914d\u7684\u6587\u4ef6\u8def\u5f84\u8fed\u4ee3\u5668<\/strong><\/h2>\n<p>files = glob.iglob(pattern)<\/p>\n<h2><strong>\u6253\u5370\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>for file in files:<\/p>\n<p>    print(file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5e76\u884c\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u6838\u5904\u7406\u5668\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5e76\u884c\u5904\u7406\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528concurrent.futures\u6a21\u5757\u5b9e\u73b0\u5e76\u884c\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import glob<\/p>\n<p>import concurrent.futures<\/p>\n<h2><strong>\u6307\u5b9a\u8def\u5f84<\/strong><\/h2>\n<p>path = &quot;\/path\/to\/directory&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u6587\u672c\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>pattern = os.path.join(path, &quot;*.txt&quot;)<\/p>\n<p>files = glob.glob(pattern)<\/p>\n<h2><strong>\u5b9a\u4e49\u5904\u7406\u6587\u4ef6\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def process_file(file):<\/p>\n<p>    with open(file, &#39;r&#39;) as f:<\/p>\n<p>        lines = f.readlines()<\/p>\n<p>        return f&quot;{file} \u6709 {len(lines)} \u884c&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b\u5e76\u884c\u5904\u7406\u6587\u4ef6<\/strong><\/h2>\n<p>with concurrent.futures.ThreadPoolExecutor() as executor:<\/p>\n<p>    results = list(executor.map(process_file, files))<\/p>\n<h2><strong>\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>for result in results:<\/p>\n<p>    print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8de8\u5e73\u53f0\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528os.path\u6a21\u5757<\/h4>\n<\/p>\n<p><p>os.path\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7ec4\u8de8\u5e73\u53f0\u8def\u5f84\u64cd\u4f5c\u51fd\u6570\uff0c\u63a8\u8350\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u5904\u7406\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u4f7f\u7528os.path.join()\u6784\u9020\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6784\u9020\u8def\u5f84<\/strong><\/h2>\n<p>path = os.path.join(&quot;path&quot;, &quot;to&quot;, &quot;directory&quot;)<\/p>\n<p>print(path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h4>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u8def\u5f84\u64cd\u4f5c\u6a21\u5757\uff0c\u63a8\u8350\u5728\u65b0\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528pathlib\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6784\u9020\u8def\u5f84<\/strong><\/h2>\n<p>path = Path(&quot;path&quot;) \/ &quot;to&quot; \/ &quot;directory&quot;<\/p>\n<p>print(path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u67e5\u627e\u672c\u5730\u6587\u4ef6\u6709\u591a\u79cd\u65b9\u6cd5\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\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7efc\u5408\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u6ce8\u91cd\u9519\u8bef\u5904\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u548c\u6027\u80fd\u4f18\u5316\uff0c\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u67e5\u627e\u548c\u64cd\u4f5c\u672c\u5730\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u8ba9\u4f60\u5728Python\u4e2d\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u5730\u67e5\u627e\u672c\u5730\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u672c\u5730\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u548c<code>glob<\/code>\u6a21\u5757\u6765\u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u67e5\u627e\u6240\u6709\u7684\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>glob<\/code>\u6a21\u5757\u4e2d\u7684<code>glob<\/code>\u51fd\u6570\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import glob\n\n# \u67e5\u627e\u5f53\u524d\u76ee\u5f55\u4e0b\u6240\u6709\u7684\u6587\u672c\u6587\u4ef6\ntext_files = glob.glob(&#39;*.txt&#39;)\nprint(text_files)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5217\u51fa\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6307\u5b9a\u76ee\u5f55\u4e2d\u641c\u7d22\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u60f3\u5728\u7279\u5b9a\u7684\u76ee\u5f55\u4e2d\u641c\u7d22\u6587\u4ef6\uff0c\u53ef\u4ee5\u7ed3\u5408<code>os<\/code>\u6a21\u5757\u4f7f\u7528<code>os.walk()<\/code>\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u904d\u5386\u6307\u5b9a\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55\uff0c\u67e5\u627e\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\n# \u6307\u5b9a\u76ee\u5f55\ndirectory = &#39;\/path\/to\/directory&#39;\n\n# \u904d\u5386\u76ee\u5f55\nfor root, dirs, files in os.walk(directory):\n    for file in files:\n        if file.endswith(&#39;.txt&#39;):\n            print(os.path.join(root, file))\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u5e2e\u52a9\u60a8\u627e\u5230\u6240\u6709\u5b50\u76ee\u5f55\u4e0b\u7684\u7279\u5b9a\u7c7b\u578b\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u67e5\u627e\u5230\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u627e\u5230\u6587\u4ef6\u540e\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5bf9\u8fd9\u4e9b\u6587\u4ef6\u8fdb\u884c\u4e00\u4e9b\u64cd\u4f5c\uff0c\u6bd4\u5982\u8bfb\u53d6\u5185\u5bb9\u6216\u8fdb\u884c\u4fee\u6539\u3002\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u540e\u88ab\u6b63\u786e\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\u3002\u6839\u636e\u9700\u6c42\uff0c\u60a8\u4e5f\u53ef\u4ee5\u5bf9\u6587\u4ef6\u8fdb\u884c\u5199\u5165\u6216\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u627e\u5230\u672c\u5730\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528glob\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404 [&hellip;]","protected":false},"author":3,"featured_media":1158172,"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\/1158167"}],"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=1158167"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158167\/revisions"}],"predecessor-version":[{"id":1158173,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158167\/revisions\/1158173"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1158172"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1158167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1158167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1158167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}