{"id":1073080,"date":"2025-01-08T11:22:02","date_gmt":"2025-01-08T03:22:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073080.html"},"modified":"2025-01-08T11:22:05","modified_gmt":"2025-01-08T03:22:05","slug":"python-%e5%a6%82%e4%bd%95%e4%bb%a5%e5%88%9b%e5%bb%ba%e7%9a%84%e6%89%93%e5%bc%80txt%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073080.html","title":{"rendered":"python \u5982\u4f55\u4ee5\u521b\u5efa\u7684\u6253\u5f00txt\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103121\/5f2bb193-6c50-4f56-aff1-832c2f3f0d42.webp\" alt=\"python \u5982\u4f55\u4ee5\u521b\u5efa\u7684\u6253\u5f00txt\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python \u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684 open() \u51fd\u6570\u6765\u521b\u5efa\u548c\u6253\u5f00 txt \u6587\u4ef6\u3001\u4f7f\u7528 with \u8bed\u53e5\u6765\u786e\u4fdd\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\u3001\u4ee5\u53ca\u4f7f\u7528 write() \u65b9\u6cd5\u5411\u6587\u4ef6\u5199\u5165\u5185\u5bb9\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528 with \u8bed\u53e5\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u5728\u7ed3\u675f\u6587\u4ef6\u64cd\u4f5c\u540e\uff0c\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u521b\u5efa\u548c\u6253\u5f00 txt \u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528 open() \u51fd\u6570\u521b\u5efa\u548c\u6253\u5f00 txt \u6587\u4ef6<\/p>\n<\/p>\n<p><p>open() \u51fd\u6570\u662f Python \u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u6253\u5f00\u6587\u4ef6\u3002\u5b83\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u5176\u4e2d\u5e38\u7528\u7684\u53c2\u6570\u6709\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u3002\u6a21\u5f0f\u53c2\u6570\u51b3\u5b9a\u4e86\u6587\u4ef6\u7684\u6253\u5f00\u65b9\u5f0f\uff0c\u5982\u53ea\u8bfb\uff08&#39;r&#39;\uff09\u3001\u5199\u5165\uff08&#39;w&#39;\uff09\u3001\u8ffd\u52a0\uff08&#39;a&#39;\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u65b0\u7684 txt \u6587\u4ef6<\/p>\n<p>file = open(&#39;example.txt&#39;, &#39;w&#39;)<\/p>\n<h2><strong>\u5199\u5165\u5185\u5bb9\u5230\u6587\u4ef6<\/strong><\/h2>\n<p>file.write(&#39;Hello, World!&#39;)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 open() \u51fd\u6570\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a &#39;example.txt&#39; \u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 &#39;w&#39; \u6a21\u5f0f\u8fdb\u884c\u5199\u5165\u64cd\u4f5c\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 write() \u65b9\u6cd5\u5411\u6587\u4ef6\u5199\u5165\u5185\u5bb9\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528 close() \u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528 with \u8bed\u53e5\u7ba1\u7406\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u4f7f\u7528 with \u8bed\u53e5\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u5728\u7ed3\u675f\u6587\u4ef6\u64cd\u4f5c\u540e\uff0c\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\u3002\u5373\u4f7f\u5728\u5199\u5165\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\uff0c\u6587\u4ef6\u4e5f\u4f1a\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 with \u8bed\u53e5\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u65b0\u7684 txt \u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    # \u5199\u5165\u5185\u5bb9\u5230\u6587\u4ef6<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 with \u8bed\u53e5\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a &#39;example.txt&#39; \u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 &#39;w&#39; \u6a21\u5f0f\u8fdb\u884c\u5199\u5165\u64cd\u4f5c\u3002\u4f7f\u7528 with \u8bed\u53e5\u540e\uff0c\u4e0d\u9700\u8981\u663e\u5f0f\u8c03\u7528 close() \u65b9\u6cd5\uff0c\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8bfb\u53d6 txt \u6587\u4ef6\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5199\u5165\u6587\u4ef6\uff0cPython \u8fd8\u53ef\u4ee5\u901a\u8fc7 open() \u51fd\u6570\u548c read() \u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 with \u8bed\u53e5\u6253\u5f00\u4e00\u4e2a\u73b0\u6709\u7684 txt \u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 with \u8bed\u53e5\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a &#39;example.txt&#39; \u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 &#39;r&#39; \u6a21\u5f0f\u8fdb\u884c\u8bfb\u53d6\u64cd\u4f5c\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 read() \u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u8ffd\u52a0\u5185\u5bb9\u5230 txt \u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\uff0c\u800c\u4e0d\u662f\u8986\u76d6\u6574\u4e2a\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528 &#39;a&#39; \u6a21\u5f0f\u8fdb\u884c\u8ffd\u52a0\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 with \u8bed\u53e5\u6253\u5f00\u4e00\u4e2a\u73b0\u6709\u7684 txt \u6587\u4ef6\u5e76\u8ffd\u52a0\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    # \u8ffd\u52a0\u5185\u5bb9\u5230\u6587\u4ef6<\/p>\n<p>    file.write(&#39;\\nAppend this line.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 with \u8bed\u53e5\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a &#39;example.txt&#39; \u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 &#39;a&#39; \u6a21\u5f0f\u8fdb\u884c\u8ffd\u52a0\u64cd\u4f5c\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 write() \u65b9\u6cd5\u5411\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528 readlines() \u65b9\u6cd5\u6216\u5faa\u73af\u904d\u5386\u6587\u4ef6\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 with \u8bed\u53e5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 with \u8bed\u53e5\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a &#39;example.txt&#39; \u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 &#39;r&#39; \u6a21\u5f0f\u8fdb\u884c\u8bfb\u53d6\u64cd\u4f5c\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 for \u5faa\u73af\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528 strip() \u65b9\u6cd5\u53bb\u9664\u6bcf\u884c\u672b\u5c3e\u7684\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u5927\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u53ef\u4ee5\u4f7f\u7528 readline() \u65b9\u6cd5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 with \u8bed\u53e5\u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    while True:<\/p>\n<p>        line = file.readline()<\/p>\n<p>        if not line:<\/p>\n<p>            break<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 with \u8bed\u53e5\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a &#39;large_file.txt&#39; \u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 &#39;r&#39; \u6a21\u5f0f\u8fdb\u884c\u8bfb\u53d6\u64cd\u4f5c\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 while \u5faa\u73af\u548c readline() \u65b9\u6cd5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528 strip() \u65b9\u6cd5\u53bb\u9664\u6bcf\u884c\u672b\u5c3e\u7684\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u6587\u4ef6\u8def\u5f84\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 os \u6a21\u5757\u4e2d\u7684\u8def\u5f84\u64cd\u4f5c\u51fd\u6570\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u517c\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_directory = os.getcwd()<\/p>\n<h2><strong>\u751f\u6210\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(current_directory, &#39;example.txt&#39;)<\/p>\n<h2><strong>\u4f7f\u7528 with \u8bed\u53e5\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u65b0\u7684 txt \u6587\u4ef6<\/strong><\/h2>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    # \u5199\u5165\u5185\u5bb9\u5230\u6587\u4ef6<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 os \u6a21\u5757\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u5e76\u751f\u6210\u6587\u4ef6\u8def\u5f84\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 with \u8bed\u53e5\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u65b0\u7684 txt \u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 write() \u65b9\u6cd5\u5411\u6587\u4ef6\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5f02\u5e38\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u5404\u79cd\u5f02\u5e38\u3002\u53ef\u4ee5\u4f7f\u7528 try-except \u8bed\u53e5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u4f7f\u7528 with \u8bed\u53e5\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u65b0\u7684 txt \u6587\u4ef6<\/p>\n<p>    with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>        # \u5199\u5165\u5185\u5bb9\u5230\u6587\u4ef6<\/p>\n<p>        file.write(&#39;Hello, World!&#39;)<\/p>\n<p>except IOError as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 try-except \u8bed\u53e5\u6355\u83b7\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u53d1\u751f\u7684 IOError \u5f02\u5e38\uff0c\u5e76\u6253\u5370\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u6587\u4ef6\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5305\u542b\u975e ASCII \u5b57\u7b26\u7684\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u3002\u53ef\u4ee5\u4f7f\u7528 encoding \u53c2\u6570\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 with \u8bed\u53e5\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u65b0\u7684 txt \u6587\u4ef6\uff0c\u5e76\u6307\u5b9a\u7f16\u7801<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    # \u5199\u5165\u5185\u5bb9\u5230\u6587\u4ef6<\/p>\n<p>    file.write(&#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 with \u8bed\u53e5\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a &#39;example.txt&#39; \u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 &#39;w&#39; \u6a21\u5f0f\u8fdb\u884c\u5199\u5165\u64cd\u4f5c\u3002\u6211\u4eec\u8fd8\u4f7f\u7528 encoding \u53c2\u6570\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u4e3a utf-8\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528 Python \u521b\u5efa\u548c\u6253\u5f00 txt \u6587\u4ef6\u3002\u6211\u4eec\u4ecb\u7ecd\u4e86\u4f7f\u7528 open() \u51fd\u6570\u521b\u5efa\u548c\u6253\u5f00\u6587\u4ef6\u3001\u4f7f\u7528 with \u8bed\u53e5\u7ba1\u7406\u6587\u4ef6\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u8ffd\u52a0\u5185\u5bb9\u5230\u6587\u4ef6\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u5904\u7406\u5927\u6587\u4ef6\u3001\u6587\u4ef6\u8def\u5f84\u5904\u7406\u3001\u5f02\u5e38\u5904\u7406\u4ee5\u53ca\u6587\u4ef6\u7f16\u7801\u7b49\u5185\u5bb9\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6253\u5f00\u4e00\u4e2a\u5df2\u521b\u5efa\u7684txt\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u6253\u5f00\u4e00\u4e2a\u5df2\u521b\u5efa\u7684txt\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u3002\u63d0\u4f9b\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\uff08\u5982&#39;r&#39;\u8868\u793a\u8bfb\u53d6\uff09\u4f5c\u4e3a\u53c2\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)\ncontent = file.read()\nfile.close()\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u66f4\u5b89\u5168\u5730\u5904\u7406\u6587\u4ef6\uff0c\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u540e\u81ea\u52a8\u5173\u95ed\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406txt\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5904\u7406txt\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u5206\u5272\u5b57\u7b26\u4e32\u548c\u8fed\u4ee3\u884c\u6765\u5b9e\u73b0\u3002\u53ef\u4ee5\u5229\u7528<code>readlines()<\/code>\u65b9\u6cd5\u6309\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:\n    lines = file.readlines()\n    for line in lines:\n        print(line.strip())  # \u53bb\u6389\u6bcf\u884c\u672b\u5c3e\u7684\u6362\u884c\u7b26\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u884c\u5206\u5272\u6210\u5355\u72ec\u7684\u5355\u8bcd\u6216\u5176\u4ed6\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u5e76\u5199\u5165txt\u6587\u4ef6\uff1f<\/strong><br \/>\u521b\u5efa\u5e76\u5199\u5165txt\u6587\u4ef6\u540c\u6837\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\uff0c\u53ea\u9700\u6307\u5b9a\u5199\u5165\u6a21\u5f0f\uff08\u5982&#39;w&#39;\u8868\u793a\u5199\u5165\uff09\u3002\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5c06\u4f1a\u88ab\u8986\u76d6\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;newfile.txt&#39;, &#39;w&#39;) as file:\n    file.write(&quot;Hello, World!\\n&quot;)\n    file.write(&quot;Welcome to Python file handling.&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u5185\u5bb9\u5199\u5165\u65b0\u7684txt\u6587\u4ef6\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684 open() \u51fd\u6570\u6765\u521b\u5efa\u548c\u6253\u5f00 txt \u6587\u4ef6\u3001\u4f7f\u7528 with \u8bed\u53e5\u6765\u786e\u4fdd\u6587\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":1073087,"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\/1073080"}],"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=1073080"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073080\/revisions"}],"predecessor-version":[{"id":1073088,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073080\/revisions\/1073088"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073087"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}