{"id":1182262,"date":"2025-01-15T19:01:10","date_gmt":"2025-01-15T11:01:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182262.html"},"modified":"2025-01-15T19:01:13","modified_gmt":"2025-01-15T11:01:13","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96d%e7%9b%98%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182262.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6d\u76d8\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130315\/1d74a6b5-2b6d-42fe-8f34-11e6229e92fd.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6d\u76d8\u6587\u4ef6\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6D\u76d8\u4e0a\u7684\u6587\u4ef6\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u6709<strong>\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528os\u548cglob\u6a21\u5757\u3001\u4f7f\u7528pathlib\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570<\/strong>\u662f\u6700\u4e3a\u57fa\u7840\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6D\u76d8\u4e0a\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u53ef\u4ee5\u8bfb\u53d6\u5404\u79cd\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u5305\u62ec\u6587\u672c\u6587\u4ef6\u548c\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u8bfb\u53d6D\u76d8\u4e0a\u6587\u672c\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6D\u76d8\u4e0a\u7684\u6587\u672c\u6587\u4ef6<\/p>\n<p>file_path = &quot;D:\\\\example.txt&quot;  # \u6587\u4ef6\u8def\u5f84\uff0c\u6ce8\u610f\u4f7f\u7528\u53cc\u53cd\u659c\u6760\u6216\u524d\u9762\u52a0r<\/p>\n<p>with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>with<\/code>\u8bed\u53e5\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002<code>open<\/code>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u8def\u5f84\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6a21\u5f0f\uff0c\u8fd9\u91cc\u4f7f\u7528<code>&#39;r&#39;<\/code>\u8868\u793a\u8bfb\u53d6\u6a21\u5f0f\uff0c<code>encoding<\/code>\u53c2\u6570\u6307\u5b9a\u4e86\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u8981\u8bfb\u53d6D\u76d8\u4e0a\u7684CSV\u6587\u4ef6\u6216Excel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u3002<code>pandas<\/code>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6D\u76d8\u4e0a\u7684CSV\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = &quot;D:\\\\example.csv&quot;<\/p>\n<p>df = pd.read_csv(file_path)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8bfb\u53d6Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6D\u76d8\u4e0a\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = &quot;D:\\\\example.xlsx&quot;<\/p>\n<p>df = pd.read_excel(file_path)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e24\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u4f7f\u7528\u4e86<code>pd.read_csv<\/code>\u548c<code>pd.read_excel<\/code>\u51fd\u6570\u6765\u8bfb\u53d6CSV\u6587\u4ef6\u548cExcel\u6587\u4ef6\uff0c\u5e76\u5c06\u5b83\u4eec\u52a0\u8f7d\u5230<code>DataFrame<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528os\u548cglob\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>os<\/code>\u548c<code>glob<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u904d\u5386D\u76d8\u4e0a\u7684\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528os\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u904d\u5386D\u76d8\u4e0a\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>directory = &quot;D:\\\\&quot;<\/p>\n<p>for filename in os.listdir(directory):<\/p>\n<p>    file_path = os.path.join(directory, filename)<\/p>\n<p>    if os.path.isfile(file_path):<\/p>\n<p>        print(f&quot;\u6587\u4ef6\u540d: {filename}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528glob\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<h2><strong>\u67e5\u627eD\u76d8\u4e0a\u7684\u6240\u6709\u6587\u672c\u6587\u4ef6<\/strong><\/h2>\n<p>file_pattern = &quot;D:\\\\*.txt&quot;<\/p>\n<p>for file_path in glob.glob(file_pattern):<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u8def\u5f84: {file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528pathlib\u5e93<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u8def\u5f84\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u6587\u4ef6\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u8bfb\u53d6D\u76d8\u4e0a\u7684\u6587\u672c\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = Path(&quot;D:\/example.txt&quot;)<\/p>\n<p>with file_path.open(&#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pathlib<\/code>\u5e93\u7684<code>Path<\/code>\u7c7b\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u66f4\u52a0\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u8bfb\u53d6D\u76d8\u4e0a\u7684\u6587\u4ef6\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570<\/strong>\u662f\u6700\u4e3a\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u8bfb\u53d6\u5404\u79cd\u7c7b\u578b\u7684\u6587\u4ef6\uff1b<strong>\u4f7f\u7528pandas\u5e93<\/strong>\u9002\u7528\u4e8e\u8bfb\u53d6\u7ed3\u6784\u5316\u6570\u636e\uff0c\u5982CSV\u6587\u4ef6\u548cExcel\u6587\u4ef6\uff1b<strong>\u4f7f\u7528os\u548cglob\u6a21\u5757<\/strong>\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u548c\u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff1b<strong>\u4f7f\u7528pathlib\u5e93<\/strong>\u63d0\u4f9b\u4e86\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u6587\u4ef6\u7cfb\u7edf\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u8bfb\u53d6\u548c\u5904\u7406D\u76d8\u4e0a\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8be6\u7ec6\u4ecb\u7ecdopen\u51fd\u6570\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u5728\u5f00\u5934\u6bb5\u843d\u4e2d\u63d0\u5230\u7684<code>open<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\uff0c\u5305\u62ec\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u548c\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6\u6587\u672c\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u662f<code>open<\/code>\u51fd\u6570\u6700\u5e38\u89c1\u7684\u7528\u9014\u4e4b\u4e00\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>&#39;r&#39;<\/code>\u6a21\u5f0f\u6765\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6D\u76d8\u4e0a\u7684\u6587\u672c\u6587\u4ef6<\/p>\n<p>file_path = &quot;D:\\\\example.txt&quot;<\/p>\n<p>with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>open<\/code>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u8def\u5f84\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6a21\u5f0f\uff0c\u8fd9\u91cc\u4f7f\u7528<code>&#39;r&#39;<\/code>\u8868\u793a\u8bfb\u53d6\u6a21\u5f0f\u3002<code>encoding<\/code>\u53c2\u6570\u6307\u5b9a\u4e86\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u5e38\u4e3a<code>&#39;utf-8&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u9010\u884c\u8bfb\u53d6\u6587\u672c\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u9010\u884c\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9010\u884c\u8bfb\u53d6D\u76d8\u4e0a\u7684\u6587\u672c\u6587\u4ef6<\/p>\n<p>file_path = &quot;D:\\\\example.txt&quot;<\/p>\n<p>with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())  # \u4f7f\u7528strip()\u53bb\u9664\u6bcf\u884c\u672b\u5c3e\u7684\u6362\u884c\u7b26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\uff0c<code>open<\/code>\u51fd\u6570\u8fd8\u53ef\u4ee5\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u4f7f\u7528<code>&#39;rb&#39;<\/code>\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6D\u76d8\u4e0a\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6<\/p>\n<p>file_path = &quot;D:\\\\example.bin&quot;<\/p>\n<p>with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>&#39;rb&#39;<\/code>\u6a21\u5f0f\u6765\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u4e0d\u9700\u8981\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528pandas\u5e93\u8bfb\u53d6\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u8bfb\u53d6CSV\u6587\u4ef6\u548cExcel\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u5927\u6587\u4ef6\u3002\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>chunksize<\/code>\u53c2\u6570\u6765\u5206\u5757\u8bfb\u53d6\uff0c\u4ece\u800c\u907f\u514d\u5185\u5b58\u4e0d\u8db3\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1. \u5206\u5757\u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5206\u5757\u8bfb\u53d6D\u76d8\u4e0a\u7684CSV\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = &quot;D:\\\\large_example.csv&quot;<\/p>\n<p>chunksize = 10000  # \u6bcf\u6b21\u8bfb\u53d610000\u884c<\/p>\n<p>for chunk in pd.read_csv(file_path, chunksize=chunksize):<\/p>\n<p>    print(chunk.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>chunksize<\/code>\u53c2\u6570\u6307\u5b9a\u6bcf\u6b21\u8bfb\u53d6\u7684\u884c\u6570\u3002<code>pd.read_csv<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u6bcf\u6b21\u8fed\u4ee3\u8fd4\u56de\u4e00\u4e2a\u6570\u636e\u5757\u3002<\/p>\n<\/p>\n<p><h4>2. \u5206\u5757\u8bfb\u53d6Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5206\u5757\u8bfb\u53d6D\u76d8\u4e0a\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = &quot;D:\\\\large_example.xlsx&quot;<\/p>\n<p>chunksize = 10000  # \u6bcf\u6b21\u8bfb\u53d610000\u884c<\/p>\n<p>for chunk in pd.read_excel(file_path, chunksize=chunksize):<\/p>\n<p>    print(chunk.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e\u8bfb\u53d6CSV\u6587\u4ef6\u7c7b\u4f3c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>chunksize<\/code>\u53c2\u6570\u6765\u5206\u5757\u8bfb\u53d6Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528os\u548cglob\u6a21\u5757\u904d\u5386\u548c\u67e5\u627e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>os<\/code>\u548c<code>glob<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u76ee\u5f55\u548c\u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u904d\u5386\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u904d\u5386D\u76d8\u4e0a\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u904d\u5386D\u76d8\u4e0a\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55<\/strong><\/h2>\n<p>directory = &quot;D:\\\\&quot;<\/p>\n<p>for root, dirs, files in os.walk(directory):<\/p>\n<p>    for name in files:<\/p>\n<p>        file_path = os.path.join(root, name)<\/p>\n<p>        print(f&quot;\u6587\u4ef6\u8def\u5f84: {file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>os.walk<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u4e09\u5143\u7ec4\uff0c\u5206\u522b\u8868\u793a\u5f53\u524d\u8def\u5f84\u3001\u5b50\u76ee\u5f55\u5217\u8868\u548c\u6587\u4ef6\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>os.path.join<\/code>\u51fd\u6570\u6784\u5efa\u5b8c\u6574\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>2. \u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>glob<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u5982\u6240\u6709\u6587\u672c\u6587\u4ef6\u6216\u6240\u6709\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<h2><strong>\u67e5\u627eD\u76d8\u4e0a\u7684\u6240\u6709\u6587\u672c\u6587\u4ef6<\/strong><\/h2>\n<p>file_pattern = &quot;D:\\\\*.txt&quot;<\/p>\n<p>for file_path in glob.glob(file_pattern):<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u8def\u5f84: {file_path}&quot;)<\/p>\n<h2><strong>\u67e5\u627eD\u76d8\u4e0a\u7684\u6240\u6709\u56fe\u7247\u6587\u4ef6<\/strong><\/h2>\n<p>file_pattern = &quot;D:\\\\*.jpg&quot;<\/p>\n<p>for file_path in glob.glob(file_pattern):<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u8def\u5f84: {file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>glob.glob<\/code>\u51fd\u6570\u6765\u67e5\u627e\u7b26\u5408\u6a21\u5f0f\u7684\u6587\u4ef6\u8def\u5f84\u3002\u6a21\u5f0f\u53ef\u4ee5\u5305\u542b\u901a\u914d\u7b26\uff0c\u5982<code>*.txt<\/code>\u8868\u793a\u6240\u6709\u6587\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528pathlib\u5e93\u64cd\u4f5c\u6587\u4ef6\u8def\u5f84<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u5e93\u63d0\u4f9b\u4e86\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u6587\u4ef6\u8def\u5f84\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h4>1. \u6784\u5efa\u6587\u4ef6\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>Path<\/code>\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u6784\u5efa\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6784\u5efaD\u76d8\u4e0a\u7684\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = Path(&quot;D:\/example.txt&quot;)<\/p>\n<p>print(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>exists<\/code>\u65b9\u6cd5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u68c0\u67e5D\u76d8\u4e0a\u7684\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>file_path = Path(&quot;D:\/example.txt&quot;)<\/p>\n<p>if file_path.exists():<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5b58\u5728&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>Path<\/code>\u7c7b\u7684<code>open<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u8bfb\u53d6D\u76d8\u4e0a\u7684\u6587\u672c\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = Path(&quot;D:\/example.txt&quot;)<\/p>\n<p>with file_path.open(&#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u904d\u5386\u76ee\u5f55<\/h4>\n<\/p>\n<p><p><code>pathlib<\/code>\u5e93\u8fd8\u63d0\u4f9b\u4e86\u904d\u5386\u76ee\u5f55\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u904d\u5386D\u76d8\u4e0a\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55<\/strong><\/h2>\n<p>directory = Path(&quot;D:\/&quot;)<\/p>\n<p>for path in directory.rglob(&#39;*&#39;):<\/p>\n<p>    print(path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>rglob<\/code>\u65b9\u6cd5\u6765\u9012\u5f52\u904d\u5386\u76ee\u5f55\uff0c<code>&#39;*&#39;<\/code>\u8868\u793a\u5339\u914d\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u67e5\u627e\u6587\u4ef6\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u67e5\u627e\u6587\u4ef6\u540d\u4e2d\u5305\u542b\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import re<\/p>\n<h2><strong>\u67e5\u627eD\u76d8\u4e0a\u6587\u4ef6\u540d\u4e2d\u5305\u542b\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>pattern = re.compile(r&#39;example&#39;)<\/p>\n<p>directory = &quot;D:\\\\&quot;<\/p>\n<p>for root, dirs, files in os.walk(directory):<\/p>\n<p>    for name in files:<\/p>\n<p>        if pattern.search(name):<\/p>\n<p>            file_path = os.path.join(root, name)<\/p>\n<p>            print(f&quot;\u6587\u4ef6\u8def\u5f84: {file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>re.compile<\/code>\u51fd\u6570\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7136\u540e\u5728\u904d\u5386\u76ee\u5f55\u65f6\u4f7f\u7528<code>pattern.search<\/code>\u65b9\u6cd5\u67e5\u627e\u6587\u4ef6\u540d\u4e2d\u5305\u542b\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2. \u67e5\u627e\u6587\u4ef6\u5185\u5bb9\u4e2d\u5305\u542b\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import re<\/p>\n<h2><strong>\u67e5\u627eD\u76d8\u4e0a\u6587\u4ef6\u5185\u5bb9\u4e2d\u5305\u542b\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>pattern = re.compile(r&#39;specific pattern&#39;)<\/p>\n<p>directory = &quot;D:\\\\&quot;<\/p>\n<p>for root, dirs, files in os.walk(directory):<\/p>\n<p>    for name in files:<\/p>\n<p>        file_path = os.path.join(root, name)<\/p>\n<p>        if os.path.isfile(file_path):<\/p>\n<p>            with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>                content = file.read()<\/p>\n<p>                if pattern.search(content):<\/p>\n<p>                    print(f&quot;\u6587\u4ef6\u8def\u5f84: {file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u904d\u5386\u76ee\u5f55\u65f6\u8bfb\u53d6\u6bcf\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528<code>pattern.search<\/code>\u65b9\u6cd5\u67e5\u627e\u6587\u4ef6\u5185\u5bb9\u4e2d\u5305\u542b\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u8bfb\u53d6\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>1. \u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u5f02\u5e38<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;D:\\\\non_existent_file.txt&quot;<\/p>\n<p>try:<\/p>\n<p>    with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u4e0d\u5b58\u5728: {file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5904\u7406\u6743\u9650\u4e0d\u8db3\u5f02\u5e38<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;D:\\\\restricted_file.txt&quot;<\/p>\n<p>try:<\/p>\n<p>    with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(f&quot;\u6743\u9650\u4e0d\u8db3: {file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e24\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7<code>FileNotFoundError<\/code>\u548c<code>PermissionError<\/code>\u5f02\u5e38\uff0c\u5e76\u6253\u5370\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u8bfb\u53d6D\u76d8\u6587\u4ef6\u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec<strong>\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528os\u548cglob\u6a21\u5757\u3001\u4f7f\u7528pathlib\u5e93\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\u6587\u4ef6<\/strong>\u4ee5\u53ca<strong>\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u5f02\u5e38<\/strong>\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u8bfb\u53d6\u548c\u5904\u7406D\u76d8\u4e0a\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u4f60\u80fd\u591f\u638c\u63e1\u5728Python\u4e2d\u8bfb\u53d6D\u76d8\u6587\u4ef6\u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u5e76\u80fd\u591f\u6839\u636e\u4e0d\u540c\u7684\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6D\u76d8\u4e0a\u7684\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6D\u76d8\u4e0a\u7684\u6587\u672c\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;D:\/yourfile.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u8bf7\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d\u6b63\u786e\uff0c\u7f16\u7801\u683c\u5f0f\u5e94\u4e0e\u6587\u4ef6\u4e00\u81f4\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u8bfb\u53d6D\u76d8\u4e0aCSV\u6587\u4ef6\u7684\u6570\u636e\uff1f<\/strong><br \/>\u8981\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = pd.read_csv(&#39;D:\/yourfile.csv&#39;)\nprint(data.head())\n<\/code><\/pre>\n<p>\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86<code>pandas<\/code>\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install pandas<\/code>\u6765\u5b89\u88c5\u3002<\/p>\n<p><strong>Python\u8bfb\u53d6D\u76d8\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u9519\u8bef\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u8be5\u60c5\u51b5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    with open(&#39;D:\/yourfile.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n        content = file.read()\n        print(content)\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u8def\u5f84\u548c\u6587\u4ef6\u540d\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u6587\u4ef6\u9519\u8bef\u800c\u5d29\u6e83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6D\u76d8\u4e0a\u7684\u6587\u4ef6\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u3001\u4f7f\u7528pandas\u5e93\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1182270,"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\/1182262"}],"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=1182262"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182262\/revisions"}],"predecessor-version":[{"id":1182274,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182262\/revisions\/1182274"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182270"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}