{"id":1106733,"date":"2025-01-08T16:42:59","date_gmt":"2025-01-08T08:42:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1106733.html"},"modified":"2025-01-08T16:43:03","modified_gmt":"2025-01-08T08:43:03","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e6%95%b4%e4%b8%aa%e6%96%87%e4%bb%b6%e5%a4%b9%e7%9a%84%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1106733.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5939\u7684\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071055\/99eb641f-88ac-46a7-8aed-7bdef93edea7.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5939\u7684\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5939\u7684\u5185\u5bb9\u53ef\u4ee5\u4f7f\u7528os\u5e93\u3001glob\u5e93\u3001pathlib\u5e93\u3002<\/strong> os\u5e93\u63d0\u4f9b\u4e86\u6700\u57fa\u7840\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u529f\u80fd\u3001glob\u5e93\u53ef\u4ee5\u4f7f\u7528\u901a\u914d\u7b26\u6765\u5339\u914d\u6587\u4ef6\u3001pathlib\u5e93\u662fPython3.4\u4e4b\u540e\u5f15\u5165\u7684\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\u5e93\u3002 \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u4e2a\u5e93\u6765\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5939\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001OS\u5e93<\/h3>\n<\/p>\n<p><p>OS\u5e93\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u5404\u79cd\u529f\u80fd\u3002\u901a\u8fc7os\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8bfb\u53d6\u6587\u4ef6\u5939\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u6587\u4ef6\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.listdir()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684\u540d\u79f0\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>        return files<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return f&quot;The directory {directory} does not exist.&quot;<\/p>\n<p>directory = &#39;path_to_your_directory&#39;<\/p>\n<p>print(list_files(directory))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.abspath()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def list_files_with_absolute_path(directory):<\/p>\n<p>    try:<\/p>\n<p>        files = os.listdir(directory)<\/p>\n<p>        absolute_files = [os.path.abspath(os.path.join(directory, file)) for file in files]<\/p>\n<p>        return absolute_files<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return f&quot;The directory {directory} does not exist.&quot;<\/p>\n<p>directory = &#39;path_to_your_directory&#39;<\/p>\n<p>print(list_files_with_absolute_path(directory))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001GLOB\u5e93<\/h3>\n<\/p>\n<p><p>Glob\u5e93\u53ef\u4ee5\u4f7f\u7528\u901a\u914d\u7b26\u6765\u5339\u914d\u6587\u4ef6\uff0c\u8fd9\u4f7f\u5f97\u67e5\u627e\u6587\u4ef6\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u901a\u914d\u7b26\u83b7\u53d6\u6587\u4ef6\u5217\u8868<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<p>def list_files_glob(directory, pattern=&#39;*&#39;):<\/p>\n<p>    files = glob.glob(f&quot;{directory}\/{pattern}&quot;)<\/p>\n<p>    return files<\/p>\n<p>directory = &#39;path_to_your_directory&#39;<\/p>\n<p>print(list_files_glob(directory))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u9012\u5f52\u67e5\u627e<\/h4>\n<\/p>\n<p><p>Glob\u5e93\u8fd8\u63d0\u4f9b\u4e86\u9012\u5f52\u67e5\u627e\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528<code><\/code>\u6765\u5339\u914d\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<p>def list_files_recursive(directory, pattern=&#39;\/*&#39;):<\/p>\n<p>    files = glob.glob(f&quot;{directory}\/{pattern}&quot;, recursive=True)<\/p>\n<p>    return files<\/p>\n<p>directory = &#39;path_to_your_directory&#39;<\/p>\n<p>print(list_files_recursive(directory))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Pathlib\u5e93<\/h3>\n<\/p>\n<p><p>Pathlib\u5e93\u662fPython3.4\u4e4b\u540e\u5f15\u5165\u7684\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u6587\u4ef6\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>Path<\/code>\u5bf9\u8c61\u7684<code>iterdir()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\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>    path = Path(directory)<\/p>\n<p>    if path.exists():<\/p>\n<p>        return [str(file) for file in path.iterdir()]<\/p>\n<p>    else:<\/p>\n<p>        return f&quot;The directory {directory} does not exist.&quot;<\/p>\n<p>directory = &#39;path_to_your_directory&#39;<\/p>\n<p>print(list_files_pathlib(directory))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u9012\u5f52\u67e5\u627e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>rglob()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u9012\u5f52\u5730\u67e5\u627e\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def list_files_recursive_pathlib(directory):<\/p>\n<p>    path = Path(directory)<\/p>\n<p>    if path.exists():<\/p>\n<p>        return [str(file) for file in path.rglob(&#39;*&#39;)]<\/p>\n<p>    else:<\/p>\n<p>        return f&quot;The directory {directory} does not exist.&quot;<\/p>\n<p>directory = &#39;path_to_your_directory&#39;<\/p>\n<p>print(list_files_recursive_pathlib(directory))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u6587\u4ef6\u5217\u8868\u540e\uff0c\u6211\u4eec\u901a\u5e38\u8fd8\u9700\u8981\u8bfb\u53d6\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file_content(file_path):<\/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>        return content<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return f&quot;The file {file_path} does not exist.&quot;<\/p>\n<p>file_path = &#39;path_to_your_file&#39;<\/p>\n<p>print(read_file_content(file_path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u9010\u884c\u8bfb\u53d6\u7684\u65b9\u5f0f\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_large_file(file_path):<\/p>\n<p>    try:<\/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())<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;The file {file_path} does not exist.&quot;)<\/p>\n<p>file_path = &#39;path_to_your_large_file&#39;<\/p>\n<p>read_large_file(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5939\u7684\u5185\u5bb9\u3002<strong>os\u5e93\u63d0\u4f9b\u4e86\u6700\u57fa\u7840\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u529f\u80fd\u3001glob\u5e93\u53ef\u4ee5\u4f7f\u7528\u901a\u914d\u7b26\u6765\u5339\u914d\u6587\u4ef6\u3001pathlib\u5e93\u662fPython3.4\u4e4b\u540e\u5f15\u5165\u7684\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\u5e93\u3002<\/strong> \u4e0d\u540c\u7684\u5e93\u6709\u4e0d\u540c\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5b8c\u6210\u4efb\u52a1\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5217\u51fa\u6587\u4ef6\u5939\u5185\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>os.listdir()<\/code>\u51fd\u6570\u6765\u5217\u51fa\u6307\u5b9a\u6587\u4ef6\u5939\u5185\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u3002\u6b64\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u540d\u79f0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_path = &#39;\u4f60\u7684\u6587\u4ef6\u5939\u8def\u5f84&#39;\ncontents = os.listdir(folder_path)\nprint(contents)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u6253\u5370\u51fa\u6307\u5b9a\u6587\u4ef6\u5939\u5185\u7684\u6240\u6709\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u82e5\u9700\u8bfb\u53d6\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3002\u4f7f\u7528<code>os.listdir()<\/code>\u5217\u51fa\u6587\u4ef6\uff0c\u518d\u901a\u8fc7\u5faa\u73af\u9010\u4e2a\u6253\u5f00\u5e76\u8bfb\u53d6\u5b83\u4eec\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_path = &#39;\u4f60\u7684\u6587\u4ef6\u5939\u8def\u5f84&#39;\nfor filename in os.listdir(folder_path):\n    if filename.endswith(&#39;.txt&#39;):  # \u53ea\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\n        with open(os.path.join(folder_path, filename), &#39;r&#39;) as file:\n            content = file.read()\n            print(content)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u6bb5\u5c06\u8bfb\u53d6\u6bcf\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6587\u4ef6\u5939\u5185\u7684\u7279\u5b9a\u7c7b\u578b\u6587\u4ef6\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u6587\u4ef6\u5939\u5185\u5bb9\u65f6\uff0c\u60a8\u53ef\u80fd\u53ea\u60f3\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u4f8b\u5982\u56fe\u50cf\u6216CSV\u6587\u4ef6\u3002\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\uff0c\u7b5b\u9009\u51fa\u6240\u9700\u6587\u4ef6\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u8bfb\u53d6\u6240\u6709CSV\u6587\u4ef6\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_path = &#39;\u4f60\u7684\u6587\u4ef6\u5939\u8def\u5f84&#39;\nfor filename in os.listdir(folder_path):\n    if filename.endswith(&#39;.csv&#39;):  # \u53ea\u5904\u7406CSV\u6587\u4ef6\n        with open(os.path.join(folder_path, filename), &#39;r&#39;) as file:\n            content = file.read()\n            print(content)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u6587\u4ef6\u5939\u5185\u7684\u7279\u5b9a\u6587\u4ef6\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5939\u7684\u5185\u5bb9\u53ef\u4ee5\u4f7f\u7528os\u5e93\u3001glob\u5e93\u3001pathlib\u5e93\u3002 os\u5e93\u63d0\u4f9b\u4e86\u6700\u57fa\u7840\u7684\u6587\u4ef6\u548c [&hellip;]","protected":false},"author":3,"featured_media":1106741,"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\/1106733"}],"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=1106733"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106733\/revisions"}],"predecessor-version":[{"id":1106742,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106733\/revisions\/1106742"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1106741"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1106733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1106733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1106733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}