{"id":1128449,"date":"2025-01-08T20:20:36","date_gmt":"2025-01-08T12:20:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1128449.html"},"modified":"2025-01-08T20:20:38","modified_gmt":"2025-01-08T12:20:38","slug":"python%e8%bf%bd%e5%8a%a0%e5%86%99%e5%85%a5txt%e6%96%87%e4%bb%b6%e5%a6%82%e4%bd%95%e6%8d%a2%e8%a1%8c%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1128449.html","title":{"rendered":"python\u8ffd\u52a0\u5199\u5165txt\u6587\u4ef6\u5982\u4f55\u6362\u884c\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095149\/21a4776b-8386-4d5f-af10-b6369c9e6d5a.webp\" alt=\"python\u8ffd\u52a0\u5199\u5165txt\u6587\u4ef6\u5982\u4f55\u6362\u884c\u7b26\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5411txt\u6587\u4ef6\u8ffd\u52a0\u5199\u5165\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u6362\u884c\u7b26\u6765\u5b9e\u73b0\u6362\u884c\u3002<\/strong> \u6362\u884c\u7b26\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u5728Python\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528<code>\\n<\/code>\u6765\u8868\u793a\u6362\u884c\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0txt\u6587\u4ef6\u7684\u8ffd\u52a0\u5199\u5165\u5e76\u6362\u884c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>\\n<\/code> \u8868\u793a\u6362\u884c\u7b26<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6362\u884c\u7b26\u901a\u5e38\u4f7f\u7528 <code>\\n<\/code> \u8868\u793a\u3002\u65e0\u8bba\u662f\u5728Windows\u3001Linux\u8fd8\u662fMacOS\u4e2d\uff0c<code>Python<\/code> \u90fd\u4f1a\u5c06 <code>\\n<\/code> \u8f6c\u6362\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u9002\u5408\u7684\u6362\u884c\u7b26\u3002\u56e0\u6b64\uff0c\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u53ea\u9700\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b <code>\\n<\/code> \u5373\u53ef\u5b9e\u73b0\u6362\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&#39;This is a new line.\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5728 <code>example.txt<\/code> \u6587\u4ef6\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u884c <code>This is a new line.<\/code>\uff0c\u5e76\u5728\u672b\u5c3e\u6dfb\u52a0\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>os.linesep<\/code> \u83b7\u53d6\u7cfb\u7edf\u9ed8\u8ba4\u6362\u884c\u7b26<\/h3>\n<\/p>\n<p><p>\u867d\u7136 <code>\\n<\/code> \u662f\u901a\u7528\u7684\u6362\u884c\u7b26\uff0c\u4f46\u5982\u679c\u4f60\u60f3\u786e\u4fdd\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7684\u6362\u884c\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u4e2d\u7684 <code>linesep<\/code> \u5c5e\u6027\u3002<code>os.linesep<\/code> \u4f1a\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u6b63\u786e\u7684\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(f&#39;This is a new line.{os.linesep}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u4ee3\u7801\u4f1a\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e0d\u540c\u9009\u62e9\u5408\u9002\u7684\u6362\u884c\u7b26\uff0c\u786e\u4fdd\u6587\u4ef6\u5728\u4e0d\u540c\u7cfb\u7edf\u4e2d\u663e\u793a\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8ffd\u52a0\u5199\u5165\u591a\u884c\u6587\u672c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u4e00\u6b21\u5199\u5165\u591a\u884c\u6587\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u884c\u5b57\u7b26\u4e32\uff08triple quotes\uff09\u6216 <code>join<\/code> \u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e24\u79cd\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u591a\u884c\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&#39;&#39;&#39;This is the first new line.<\/p>\n<p>This is the second new line.<\/p>\n<p>This is the third new line.<\/p>\n<p>&#39;&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528 <code>join<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [<\/p>\n<p>    &quot;This is the first new line.&quot;,<\/p>\n<p>    &quot;This is the second new line.&quot;,<\/p>\n<p>    &quot;This is the third new line.&quot;<\/p>\n<p>]<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&#39;\\n&#39;.join(lines) + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4e24\u79cd\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5730\u5c06\u591a\u884c\u6587\u672c\u8ffd\u52a0\u5230\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u6bcf\u884c\u672b\u5c3e\u6dfb\u52a0\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 <code>print<\/code> \u51fd\u6570\u5199\u5165\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>print<\/code> \u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5199\u5165\u6587\u4ef6\uff0c\u5e76\u4e14\u5b83\u4f1a\u81ea\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>file<\/code> \u53c2\u6570\u6307\u5b9a\u5199\u5165\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    print(&#39;This is a new line using print.&#39;, file=file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u7b80\u6d01\u660e\u4e86\uff0c\u5e76\u4e14\u65e0\u9700\u624b\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5927\u6587\u4ef6\u7684\u8ffd\u52a0\u5199\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5206\u6279\u6b21\u8ffd\u52a0\u5199\u5165\u4ee5\u907f\u514d\u5360\u7528\u8fc7\u591a\u5185\u5b58\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u5206\u6279\u6b21\u8ffd\u52a0\u5199\u5165\u5927\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def append_large_file(filename, data, batch_size=1000):<\/p>\n<p>    with open(filename, &#39;a&#39;) as file:<\/p>\n<p>        for i in range(0, len(data), batch_size):<\/p>\n<p>            batch = data[i:i+batch_size]<\/p>\n<p>            file.write(&#39;\\n&#39;.join(batch) + &#39;\\n&#39;)<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>data = [f&#39;This is line {i}&#39; for i in range(10000)]<\/p>\n<p>append_large_file(&#39;large_example.txt&#39;, data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>append_large_file<\/code> \u51fd\u6570\uff0c\u7528\u4e8e\u5206\u6279\u6b21\u5c06\u6570\u636e\u8ffd\u52a0\u5199\u5165\u6587\u4ef6\u3002<code>batch_size<\/code> \u53c2\u6570\u6307\u5b9a\u6bcf\u6b21\u5199\u5165\u7684\u884c\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5411txt\u6587\u4ef6\u8ffd\u52a0\u5199\u5165\u5e76\u6362\u884c\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u4f7f\u7528 <code>\\n<\/code>\u3001<code>os.linesep<\/code>\u3001\u591a\u884c\u5b57\u7b26\u4e32\u3001<code>join<\/code> \u65b9\u6cd5\u4ee5\u53ca <code>print<\/code> \u51fd\u6570\u90fd\u662f\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5<\/strong>\u3002\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u5206\u6279\u6b21\u5199\u5165\u53ef\u4ee5\u6709\u6548\u907f\u514d\u5185\u5b58\u5360\u7528\u8fc7\u591a\u7684\u95ee\u9898\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0txt\u6587\u4ef6\u7684\u8ffd\u52a0\u5199\u5165\u5e76\u6362\u884c\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u5173\u952e\u5728\u4e8e\u7406\u89e3\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u7075\u6d3b\u8fd0\u7528\u4ee5\u6ee1\u8db3\u5177\u4f53\u9700\u6c42\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1Python\u4e2d\u5411txt\u6587\u4ef6\u8ffd\u52a0\u5199\u5165\u5e76\u6362\u884c\u7684\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u5728\u8ffd\u52a0\u5199\u5165txt\u6587\u4ef6\u65f6\u5b9e\u73b0\u6362\u884c\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u5982\u679c\u5e0c\u671b\u5728\u8ffd\u52a0\u5199\u5165\u5185\u5bb9\u65f6\u5b9e\u73b0\u6362\u884c\uff0c\u53ef\u4ee5\u5728\u5f85\u5199\u5165\u7684\u5b57\u7b26\u4e32\u672b\u5c3e\u6dfb\u52a0\u6362\u884c\u7b26<code>\\n<\/code>\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>file.write(&quot;\u5185\u5bb9\\n&quot;)<\/code>\uff0c\u8fd9\u6837\u6bcf\u6b21\u5199\u5165\u540e\u90fd\u4f1a\u5728\u5185\u5bb9\u7684\u540e\u9762\u6dfb\u52a0\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u4ece\u800c\u786e\u4fdd\u65b0\u5185\u5bb9\u4f1a\u4ece\u65b0\u7684\u4e00\u884c\u5f00\u59cb\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5728\u8ffd\u52a0\u5199\u5165txt\u6587\u4ef6\u65f6\u4e0d\u4f1a\u91cd\u590d\u5199\u5165\u76f8\u540c\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u4e3a\u4e86\u907f\u514d\u91cd\u590d\u5199\u5165\u76f8\u540c\u5185\u5bb9\uff0c\u53ef\u4ee5\u5728\u5199\u5165\u4e4b\u524d\u5148\u8bfb\u53d6\u6587\u4ef6\u7684\u5185\u5bb9\u5e76\u8fdb\u884c\u68c0\u67e5\u3002\u53ef\u4ee5\u4f7f\u7528<code>file.read()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u968f\u540e\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u6765\u51b3\u5b9a\u662f\u5426\u5199\u5165\u65b0\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u91cd\u590d\u6570\u636e\u7684\u51fa\u73b0\u3002<\/p>\n<p><strong>\u5728\u8ffd\u52a0\u5199\u5165txt\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u672c\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8ffd\u52a0\u5199\u5165\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a<code>encoding<\/code>\u53c2\u6570\u6765\u5904\u7406\u6587\u672c\u7f16\u7801\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>open(&quot;\u6587\u4ef6\u540d.txt&quot;, &quot;a&quot;, encoding=&quot;utf-8&quot;)<\/code>\u53ef\u4ee5\u786e\u4fdd\u6587\u672c\u6309\u7167UTF-8\u7f16\u7801\u8fdb\u884c\u5199\u5165\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u6587\u672c\u975e\u5e38\u91cd\u8981\uff0c\u786e\u4fdd\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5411txt\u6587\u4ef6\u8ffd\u52a0\u5199\u5165\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u6362\u884c\u7b26\u6765\u5b9e\u73b0\u6362\u884c\u3002 \u6362\u884c\u7b26\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u6709\u6240\u4e0d [&hellip;]","protected":false},"author":3,"featured_media":1128452,"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\/1128449"}],"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=1128449"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128449\/revisions"}],"predecessor-version":[{"id":1128453,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128449\/revisions\/1128453"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1128452"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1128449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1128449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1128449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}