{"id":1110273,"date":"2025-01-08T17:18:08","date_gmt":"2025-01-08T09:18:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110273.html"},"modified":"2025-01-08T17:18:10","modified_gmt":"2025-01-08T09:18:10","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e6%96%87%e4%bb%b6%e5%a4%b9%e4%b8%8b%e7%9a%84%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1110273.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u6253\u5f00\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073156\/9fa54e18-8915-45b9-bf38-c7e45d3dad2d.webp\" alt=\"python\u4e2d\u5982\u4f55\u6253\u5f00\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u6253\u5f00\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528glob\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3001\u4f7f\u7528open\u51fd\u6570\u7b49\u3002<\/strong>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/p>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u6a21\u5757\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u5904\u7406\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684\u529f\u80fd\u3002\u4f7f\u7528os\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5730\u6253\u5f00\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>def list_files_in_directory(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>        print(f&quot;The directory {directory} does not exist.&quot;)<\/p>\n<p>        return []<\/p>\n<h2><strong>\u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>def read_file_content(file_path):<\/p>\n<p>    try:<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>        return content<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;The file {file_path} does not exist.&quot;)<\/p>\n<p>    except IOError:<\/p>\n<p>        print(f&quot;Could not read file {file_path}.&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>directory_path = &#39;your_directory_path_here&#39;<\/p>\n<p>files = list_files_in_directory(directory_path)<\/p>\n<p>for file_name in files:<\/p>\n<p>    file_path = os.path.join(directory_path, file_name)<\/p>\n<p>    content = read_file_content(file_path)<\/p>\n<p>    print(f&quot;Content of {file_name}: {content}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>os.listdir<\/code>\u51fd\u6570\u5217\u51fa\u6307\u5b9a\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>os.path.join<\/code>\u6784\u5efa\u6587\u4ef6\u8def\u5f84\uff0c\u7136\u540e\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528glob\u6a21\u5757<\/p>\n<\/p>\n<p><p>glob\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u6587\u4ef6\u8def\u5f84\u5339\u914d\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4f7f\u7528\u901a\u914d\u7b26\u5339\u914d\u6587\u4ef6\u540d\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709txt\u6587\u4ef6<\/strong><\/h2>\n<p>def list_txt_files_in_directory(directory):<\/p>\n<p>    files = glob.glob(f&quot;{directory}\/*.txt&quot;)<\/p>\n<p>    return files<\/p>\n<h2><strong>\u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>def read_file_content(file_path):<\/p>\n<p>    try:<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>        return content<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;The file {file_path} does not exist.&quot;)<\/p>\n<p>    except IOError:<\/p>\n<p>        print(f&quot;Could not read file {file_path}.&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>directory_path = &#39;your_directory_path_here&#39;<\/p>\n<p>txt_files = list_txt_files_in_directory(directory_path)<\/p>\n<p>for file_path in txt_files:<\/p>\n<p>    content = read_file_content(file_path)<\/p>\n<p>    print(f&quot;Content of {file_path}: {content}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>glob.glob<\/code>\u51fd\u6570\u5339\u914d\u6307\u5b9a\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709<code>.txt<\/code>\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/p>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u5904\u7406\u6a21\u5757\uff0c\u76f8\u8f83\u4e8eos\u6a21\u5757\u548cglob\u6a21\u5757\uff0cpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>def list_files_in_directory(directory):<\/p>\n<p>    try:<\/p>\n<p>        path = Path(directory)<\/p>\n<p>        files = list(path.iterdir())<\/p>\n<p>        return files<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;The directory {directory} does not exist.&quot;)<\/p>\n<p>        return []<\/p>\n<h2><strong>\u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>def read_file_content(file_path):<\/p>\n<p>    try:<\/p>\n<p>        with file_path.open(&#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>        return content<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;The file {file_path} does not exist.&quot;)<\/p>\n<p>    except IOError:<\/p>\n<p>        print(f&quot;Could not read file {file_path}.&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>directory_path = &#39;your_directory_path_here&#39;<\/p>\n<p>files = list_files_in_directory(directory_path)<\/p>\n<p>for file_path in files:<\/p>\n<p>    content = read_file_content(file_path)<\/p>\n<p>    print(f&quot;Content of {file_path}: {content}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>Path.iterdir<\/code>\u65b9\u6cd5\u5217\u51fa\u6307\u5b9a\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>Path.open<\/code>\u65b9\u6cd5\u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528open\u51fd\u6570<\/p>\n<\/p>\n<p><p>open\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6253\u5f00\u6587\u4ef6\u7684\u51fd\u6570\uff0c\u867d\u7136\u5728\u4e0a\u8ff0\u65b9\u6cd5\u4e2d\u5df2\u7ecf\u4f7f\u7528\u8fc7\uff0c\u4f46\u8fd9\u91cc\u5355\u72ec\u5217\u51fa\u662f\u4e3a\u4e86\u5f3a\u8c03\u5176\u91cd\u8981\u6027\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>def read_file_content(file_path):<\/p>\n<p>    try:<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>        return content<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;The file {file_path} does not exist.&quot;)<\/p>\n<p>    except IOError:<\/p>\n<p>        print(f&quot;Could not read file {file_path}.&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>file_path = &#39;your_file_path_here&#39;<\/p>\n<p>content = read_file_content(file_path)<\/p>\n<p>print(f&quot;Content of {file_path}: {content}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6253\u5f00\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528os\u6a21\u5757\u3001glob\u6a21\u5757\u3001pathlib\u6a21\u5757\u6216\u76f4\u63a5\u4f7f\u7528open\u51fd\u6570\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002os\u6a21\u5757\u9002\u5408\u57fa\u672c\u7684\u6587\u4ef6\u64cd\u4f5c\uff0cglob\u6a21\u5757\u9002\u5408\u9700\u8981\u4f7f\u7528\u901a\u914d\u7b26\u5339\u914d\u6587\u4ef6\u540d\u7684\u573a\u666f\uff0cpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u5f0f\uff0c\u800copen\u51fd\u6570\u5219\u662f\u6700\u57fa\u7840\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u8bfb\u53d6\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5f02\u5e38\u5904\u7406\uff0c\u80fd\u591f\u66f4\u597d\u5730\u5b9e\u73b0\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\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\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>os.listdir()<\/code>\u65b9\u6cd5\u6765\u5217\u51fa\u6307\u5b9a\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>os.listdir(&#39;\u6587\u4ef6\u5939\u8def\u5f84&#39;)<\/code>\u53ef\u4ee5\u83b7\u53d6\u8be5\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u7684\u540d\u79f0\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u904d\u5386\u8fd9\u4e2a\u5217\u8868\u6765\u5904\u7406\u6bcf\u4e2a\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5f00\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u5728Python\u4e2d\u53ea\u6253\u5f00\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff08\u5982\u6587\u672c\u6587\u4ef6\u6216CSV\u6587\u4ef6\uff09\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>fnmatch<\/code>\u6a21\u5757\u6765\u7b5b\u9009\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>fnmatch.filter()<\/code>\u65b9\u6cd5\u6765\u5339\u914d\u7279\u5b9a\u6269\u5c55\u540d\u7684\u6587\u4ef6\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u6587\u4ef6\u5939\u4e2d\u6240\u9700\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1f<\/strong><br \/>\u5728\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u548c\u6a21\u5f0f\uff08\u5982&#39;r&#39;\u8868\u793a\u53ea\u8bfb\uff09\uff0c\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u3002\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9\uff0c\u800c<code>readlines()<\/code>\u5219\u53ef\u4ee5\u5c06\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u4f5c\u4e3a\u4e00\u4e2a\u5217\u8868\u5143\u7d20\u8fd4\u56de\u3002\u8fd9\u6837\uff0c\u60a8\u4fbf\u80fd\u591f\u5bf9\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u6253\u5f00\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528glob\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1110278,"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\/1110273"}],"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=1110273"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110273\/revisions"}],"predecessor-version":[{"id":1110280,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110273\/revisions\/1110280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1110278"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1110273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1110273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1110273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}