{"id":1115702,"date":"2025-01-08T18:09:21","date_gmt":"2025-01-08T10:09:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1115702.html"},"modified":"2025-01-08T18:09:24","modified_gmt":"2025-01-08T10:09:24","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%e7%9a%84%e5%8d%95%e4%b8%aa%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1115702.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u5939\u4e0b\u7684\u5355\u4e2a\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080348\/b6ef650e-6151-496f-b58b-ea1f8825cb13.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u5939\u4e0b\u7684\u5355\u4e2a\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u6587\u4ef6\u5939\u4e0b\u7684\u5355\u4e2a\u6587\u4ef6\uff0c\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528glob\u6a21\u5757\u3001\u4f7f\u7528Pathlib\u5e93\u3001\u4f7f\u7528pandas\u5e93\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong>\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u901a\u8fc7<code>os<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.listdir()<\/code>\u65b9\u6cd5\u5217\u51fa\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u7136\u540e\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u6765\u7b5b\u9009\u51fa\u6211\u4eec\u9700\u8981\u7684\u7279\u5b9a\u6587\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6765\u6253\u5f00\u8be5\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u5b9e\u73b0\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.listdir()<\/code>\u5217\u51fa\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u7136\u540e\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u6765\u9009\u62e9\u7279\u5b9a\u6587\u4ef6\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;path\/to\/your\/folder&#39;<\/p>\n<h2><strong>\u5217\u51fa\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55<\/strong><\/h2>\n<p>files = os.listdir(folder_path)<\/p>\n<h2><strong>\u904d\u5386\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55<\/strong><\/h2>\n<p>for file_name in files:<\/p>\n<p>    # \u62fc\u63a5\u5b8c\u6574\u8def\u5f84<\/p>\n<p>    file_path = os.path.join(folder_path, file_name)<\/p>\n<p>    # \u5224\u65ad\u662f\u5426\u662f\u6587\u4ef6<\/p>\n<p>    if os.path.isfile(file_path):<\/p>\n<p>        # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/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>os.listdir()<\/code>\u5217\u51fa\u4e86\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u7136\u540e\u901a\u8fc7<code>os.path.join()<\/code>\u62fc\u63a5\u5b8c\u6574\u8def\u5f84\uff0c\u4f7f\u7528<code>os.path.isfile()<\/code>\u5224\u65ad\u662f\u5426\u662f\u6587\u4ef6\uff0c\u6700\u540e\u901a\u8fc7<code>open()<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528glob\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>glob<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u6765\u5339\u914d\u6587\u4ef6\u8def\u5f84\u540d\uff0c\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u6587\u4ef6\u540d\u5339\u914d\u529f\u80fd\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u9700\u8981\u6309\u6a21\u5f0f\u5339\u914d\u6587\u4ef6\u7684\u60c5\u51b5\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;path\/to\/your\/folder&#39;<\/p>\n<h2><strong>\u5339\u914d\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>file_list = glob.glob(folder_path + &#39;\/*&#39;)<\/p>\n<h2><strong>\u904d\u5386\u6240\u6709\u5339\u914d\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>for file_path in file_list:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/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>glob.glob()<\/code>\u5339\u914d\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u5217\u8868\u3002\u7136\u540e\u904d\u5386\u8be5\u5217\u8868\u5e76\u8bfb\u53d6\u6bcf\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pathlib\u5e93<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\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;path\/to\/your\/folder&#39;)<\/p>\n<h2><strong>\u5217\u51fa\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>file_list = list(folder_path.glob(&#39;*&#39;))<\/p>\n<h2><strong>\u904d\u5386\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>for file_path in file_list:<\/p>\n<p>    # \u5224\u65ad\u662f\u5426\u662f\u6587\u4ef6<\/p>\n<p>    if file_path.is_file():<\/p>\n<p>        # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/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>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>Path<\/code>\u7c7b\u8868\u793a\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\uff0c<code>glob(&#39;*&#39;)<\/code>\u65b9\u6cd5\u5339\u914d\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u7136\u540e\u901a\u8fc7<code>is_file()<\/code>\u65b9\u6cd5\u5224\u65ad\u662f\u5426\u662f\u6587\u4ef6\uff0c\u6700\u540e\u901a\u8fc7<code>open()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528pandas\u5e93\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u7684\u662f\u7ed3\u6784\u5316\u6570\u636e\u6587\u4ef6\uff08\u5982CSV\u3001Excel\u7b49\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = &#39;path\/to\/your\/file.csv&#39;<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>data = pd.read_csv(file_path)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>read_csv()<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u52a0\u8f7d\u4e3aDataFrame\u5bf9\u8c61\uff0c\u4fbf\u4e8e\u540e\u7eed\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u603b\u7ed3\u800c\u8a00\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8bfb\u53d6\u6587\u4ef6\u5939\u4e0b\u7684\u5355\u4e2a\u6587\u4ef6\uff0c\u5305\u62ec<code>os<\/code>\u6a21\u5757\u3001<code>glob<\/code>\u6a21\u5757\u3001<code>pathlib<\/code>\u5e93\u548c<code>pandas<\/code>\u5e93\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u6587\u4ef6\u8bfb\u53d6\u548c\u5185\u5bb9\u5904\u7406\u3002\u65e0\u8bba\u662f\u6587\u4ef6\u540d\u5339\u914d\u3001\u8def\u5f84\u64cd\u4f5c\uff0c\u8fd8\u662f\u7ed3\u6784\u5316\u6570\u636e\u5904\u7406\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u652f\u6301\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\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u8bfb\u53d6\u7279\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e<code>os<\/code>\u6a21\u5757\u6216<code>pathlib<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84\u5e76\u786e\u4fdd\u6587\u4ef6\u5b58\u5728\u3002\u7136\u540e\uff0c\u4f7f\u7528\u6253\u5f00\u6587\u4ef6\u7684\u65b9\u6cd5\uff08\u4f8b\u5982<code>open()<\/code>\uff09\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\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;your\/folder\/path&#39;\nfile_name = &#39;your_file.txt&#39;\nfile_path = os.path.join(folder_path, file_name)\n\nwith open(file_path, &#39;r&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u8bfb\u53d6\u6587\u4ef6\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u672a\u627e\u5230\u3001\u6743\u9650\u4e0d\u8db3\u6216\u5176\u4ed6I\/O\u9519\u8bef\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    with open(file_path, &#39;r&#39;) as file:\n        content = file.read()\nexcept FileNotFoundError:\n    print(f&quot;\u6587\u4ef6 {file_name} \u672a\u627e\u5230\u3002&quot;)\nexcept PermissionError:\n    print(f&quot;\u6ca1\u6709\u6743\u9650\u8bfb\u53d6\u6587\u4ef6 {file_name}\u3002&quot;)\nexcept Exception as e:\n    print(f&quot;\u53d1\u751f\u4e86\u4e00\u4e2a\u9519\u8bef: {e}&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u5939\u4e2d\u591a\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u8bfb\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.listdir()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5217\u8868\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u6587\u4ef6\u8fdb\u884c\u5faa\u73af\u8bfb\u53d6\u3002\u786e\u4fdd\u6839\u636e\u9700\u8981\u7b5b\u9009\u6587\u4ef6\u7c7b\u578b\uff0c\u4ee5\u907f\u514d\u8bfb\u53d6\u4e0d\u5fc5\u8981\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">folder_path = &#39;your\/folder\/path&#39;\n\nfor file_name in os.listdir(folder_path):\n    if file_name.endswith(&#39;.txt&#39;):  # \u53ea\u8bfb\u53d6.txt\u6587\u4ef6\n        file_path = os.path.join(folder_path, file_name)\n        with open(file_path, &#39;r&#39;) as file:\n            content = file.read()\n            print(f&quot;\u5185\u5bb9\u6765\u81ea {file_name}:\\n{content}\\n&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u6587\u4ef6\u5939\u4e0b\u7684\u5355\u4e2a\u6587\u4ef6\uff0c\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528glob\u6a21\u5757\u3001\u4f7f\u7528Pathlib\u5e93\u3001\u4f7f\u7528pandas\u5e93\u8bfb [&hellip;]","protected":false},"author":3,"featured_media":1115716,"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\/1115702"}],"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=1115702"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115702\/revisions"}],"predecessor-version":[{"id":1115719,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115702\/revisions\/1115719"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1115716"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1115702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1115702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1115702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}