{"id":1121992,"date":"2025-01-08T19:15:51","date_gmt":"2025-01-08T11:15:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1121992.html"},"modified":"2025-01-08T19:15:54","modified_gmt":"2025-01-08T11:15:54","slug":"python%e5%a6%82%e4%bd%95%e4%b8%80%e6%ac%a1%e5%ad%98%e5%85%a5%e5%a4%9a%e4%b8%aatxt%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1121992.html","title":{"rendered":"python\u5982\u4f55\u4e00\u6b21\u5b58\u5165\u591a\u4e2atxt\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084016\/0a775e5c-3750-4de2-b468-c0e8ad295869.webp\" alt=\"python\u5982\u4f55\u4e00\u6b21\u5b58\u5165\u591a\u4e2atxt\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u4e00\u6b21\u5b58\u5165\u591a\u4e2atxt\u6587\u4ef6\u7684\u65b9\u6cd5<\/strong>\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<strong>\u521b\u5efa\u6570\u636e\u5217\u8868\u3001\u4f7f\u7528\u5faa\u73af\u3001\u6253\u5f00\u6587\u4ef6\u3001\u5199\u5165\u6570\u636e<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5faa\u73af<\/strong>\u662f\u5173\u952e\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u6587\u4ef6\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528Python\u7684\u5185\u7f6e\u529f\u80fd\u548c\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e2a\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efa\u6570\u636e\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u4e2atxt\u6587\u4ef6\u65f6\uff0c\u9996\u5148\u9700\u8981\u6574\u7406\u597d\u6570\u636e\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u5c06\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u6216\u5b57\u5178\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u4e00\u4e2a\u6587\u4ef6\u7684\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u65b9\u4fbf\u7ba1\u7406\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_list = [<\/p>\n<p>    &quot;This is the content for file 1.&quot;,<\/p>\n<p>    &quot;This is the content for file 2.&quot;,<\/p>\n<p>    &quot;This is the content for file 3.&quot;<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u53ef\u4ee5\u5927\u5e45\u7b80\u5316\u4ee3\u7801\uff0c\u4f7f\u5176\u80fd\u591f\u81ea\u52a8\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u3002\u5faa\u73af\u7684\u5173\u952e\u5728\u4e8e\u904d\u5386\u6570\u636e\u5217\u8868\uff0c\u5e76\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u5904\u7406\u4e00\u4e2a\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u6d01\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i, data in enumerate(data_list):<\/p>\n<p>    filename = f&quot;file_{i+1}.txt&quot;<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<strong>enumerate<\/strong>\u51fd\u6570\u4e0d\u4ec5\u904d\u5386\u6570\u636e\u5217\u8868\uff0c\u8fd8\u63d0\u4f9b\u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u7528\u4e8e\u751f\u6210\u6587\u4ef6\u540d\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u6570\u636e\u5217\u8868\u6709\u591a\u5c11\u5143\u7d20\uff0c\u90fd\u80fd\u81ea\u52a8\u751f\u6210\u76f8\u5e94\u6570\u91cf\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6253\u5f00\u6587\u4ef6<\/p>\n<\/p>\n<p><p>Python\u7684<strong>open<\/strong>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u6253\u5f00\u6587\u4ef6\u3002\u5e38\u7528\u7684\u6a21\u5f0f\u5305\u62ec<strong>&#39;w&#39;<\/strong>\uff08\u5199\u5165\u6a21\u5f0f\uff09\u548c<strong>&#39;a&#39;<\/strong>\uff08\u8ffd\u52a0\u6a21\u5f0f\uff09\u3002\u5199\u5165\u6a21\u5f0f\u9002\u7528\u4e8e\u521b\u5efa\u65b0\u6587\u4ef6\u6216\u8986\u76d6\u73b0\u6709\u6587\u4ef6\uff0c\u800c\u8ffd\u52a0\u6a21\u5f0f\u9002\u5408\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6a21\u5f0f\u975e\u5e38\u91cd\u8981\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;This will overwrite the file.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5199\u5165\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5199\u5165\u6570\u636e\u662f\u6574\u4e2a\u8fc7\u7a0b\u7684\u6838\u5fc3\u6b65\u9aa4\u3002\u901a\u8fc7<strong>file.write()<\/strong>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u4e3a\u4e86\u4fdd\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\uff0c\u901a\u5e38\u4f7f\u7528<strong>with<\/strong>\u8bed\u53e5\u7ba1\u7406\u6587\u4ef6\u4e0a\u4e0b\u6587\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u5728\u5199\u5165\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\uff0c\u6587\u4ef6\u4e5f\u4f1a\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\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(&quot;This will be appended to the file.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5b9e\u4f8b\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c06\u4ee5\u4e0a\u6b65\u9aa4\u7ed3\u5408\u8d77\u6765\uff0c\u5b9e\u73b0\u4e00\u6b21\u6027\u5b58\u5165\u591a\u4e2atxt\u6587\u4ef6\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_list = [<\/p>\n<p>    &quot;Content for the first file.&quot;,<\/p>\n<p>    &quot;Content for the second file.&quot;,<\/p>\n<p>    &quot;Content for the third file.&quot;<\/p>\n<p>]<\/p>\n<p>for i, data in enumerate(data_list):<\/p>\n<p>    filename = f&quot;file_{i+1}.txt&quot;<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0cPython\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u591a\u4e2atxt\u6587\u4ef6\u7684\u5199\u5165\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u6279\u91cf\u751f\u6210\u62a5\u544a\u3001\u65e5\u5fd7\u8fd8\u662f\u6570\u636e\u5907\u4efd\uff0c\u8fd9\u79cd\u65b9\u6cd5\u90fd\u80fd\u6781\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6570\u636e\u51c6\u5907\u4e0e\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u4e2atxt\u6587\u4ef6\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u3002\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u591a\u4e2a\u6765\u6e90\uff0c\u5982\u6570\u636e\u5e93\u3001API\u3001\u7528\u6237\u8f93\u5165\u7b49\u3002\u65e0\u8bba\u6570\u636e\u6765\u6e90\u5982\u4f55\uff0c\u5173\u952e\u5728\u4e8e\u5982\u4f55\u7ec4\u7ec7\u548c\u7ba1\u7406\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u6570\u636e\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u5217\u8868\u662f\u6700\u5e38\u89c1\u7684\u5b58\u50a8\u65b9\u5f0f\u3002\u901a\u8fc7\u5217\u8868\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u548c\u8bbf\u95ee\u6bcf\u4e2a\u6570\u636e\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_list = [<\/p>\n<p>    &quot;First data entry&quot;,<\/p>\n<p>    &quot;Second data entry&quot;,<\/p>\n<p>    &quot;Third data entry&quot;<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u6570\u636e\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u5b57\u5178\u662f\u4e00\u79cd\u66f4\u9ad8\u7ea7\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u989d\u5916\u4fe1\u606f\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u6587\u4ef6\u540d\u548c\u6570\u636e\u5185\u5bb9\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_dict = {<\/p>\n<p>    &quot;file_1.txt&quot;: &quot;First data entry&quot;,<\/p>\n<p>    &quot;file_2.txt&quot;: &quot;Second data entry&quot;,<\/p>\n<p>    &quot;file_3.txt&quot;: &quot;Third data entry&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u660e\u786e\u6307\u5b9a\u6bcf\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u907f\u514d\u56e0\u6587\u4ef6\u540d\u51b2\u7a81\u5bfc\u81f4\u7684\u6570\u636e\u8986\u76d6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6587\u4ef6\u64cd\u4f5c\u57fa\u7840<\/h3>\n<\/p>\n<p><p>\u6587\u4ef6\u64cd\u4f5c\u662f\u7f16\u7a0b\u4e2d\u7684\u57fa\u672c\u6280\u80fd\uff0c\u638c\u63e1\u6587\u4ef6\u7684\u6253\u5f00\u3001\u5199\u5165\u548c\u5173\u95ed\u64cd\u4f5c\u975e\u5e38\u91cd\u8981\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u4f7f\u6587\u4ef6\u5904\u7406\u53d8\u5f97\u7b80\u5355\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u6253\u5f00\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6253\u5f00\u6587\u4ef6\u662f\u6587\u4ef6\u64cd\u4f5c\u7684\u7b2c\u4e00\u6b65\u3002Python\u7684<strong>open<\/strong>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u6253\u5f00\u6587\u4ef6\uff0c\u5e38\u7528\u7684\u6a21\u5f0f\u6709\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>&#39;r&#39;<\/strong>\uff1a\u8bfb\u6a21\u5f0f<\/li>\n<li><strong>&#39;w&#39;<\/strong>\uff1a\u5199\u6a21\u5f0f\uff08\u8986\u76d6\uff09<\/li>\n<li><strong>&#39;a&#39;<\/strong>\uff1a\u8ffd\u52a0\u6a21\u5f0f<\/li>\n<\/ul>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6253\u5f00\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;w&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u5199\u5165\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5199\u5165\u6587\u4ef6\u662f\u6587\u4ef6\u64cd\u4f5c\u7684\u6838\u5fc3\u6b65\u9aa4\u3002\u901a\u8fc7<strong>file.write()<\/strong>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5199\u5165\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.write(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3 \u5173\u95ed\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5173\u95ed\u6587\u4ef6\u662f\u6587\u4ef6\u64cd\u4f5c\u7684\u6700\u540e\u4e00\u6b65\u3002\u901a\u8fc7<strong>file.close()<\/strong>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5173\u95ed\u6587\u4ef6\uff0c\u91ca\u653e\u8d44\u6e90\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5173\u95ed\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5faa\u73af\u5904\u7406\u591a\u4e2a\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u53ef\u4ee5\u5927\u5e45\u7b80\u5316\u4ee3\u7801\uff0c\u4f7f\u5176\u80fd\u591f\u81ea\u52a8\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed3\u5408\u5faa\u73af\u548c\u6587\u4ef6\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_list = [<\/p>\n<p>    &quot;Data for file 1&quot;,<\/p>\n<p>    &quot;Data for file 2&quot;,<\/p>\n<p>    &quot;Data for file 3&quot;<\/p>\n<p>]<\/p>\n<p>for i, data in enumerate(data_list):<\/p>\n<p>    filename = f&quot;file_{i+1}.txt&quot;<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<strong>enumerate<\/strong>\u51fd\u6570\u4e0d\u4ec5\u904d\u5386\u6570\u636e\u5217\u8868\uff0c\u8fd8\u63d0\u4f9b\u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u7528\u4e8e\u751f\u6210\u6587\u4ef6\u540d\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u6570\u636e\u5217\u8868\u6709\u591a\u5c11\u5143\u7d20\uff0c\u90fd\u80fd\u81ea\u52a8\u751f\u6210\u76f8\u5e94\u6570\u91cf\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u975e\u5e38\u91cd\u8981\u3002\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u907f\u514d\u7a0b\u5e8f\u56e0\u610f\u5916\u60c5\u51b5\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u6355\u83b7\u6587\u4ef6\u64cd\u4f5c\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<strong>try-except<\/strong>\u8bed\u53e5\uff0c\u53ef\u4ee5\u6355\u83b7\u6587\u4ef6\u64cd\u4f5c\u4e2d\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u6587\u4ef6\u65e0\u6cd5\u6253\u5f00\u6216\u5199\u5165\u65f6\uff0c\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>        file.write(&quot;Hello, World!&quot;)<\/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><h4>4.2 \u6355\u83b7\u6570\u636e\u5904\u7406\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u4e5f\u53ef\u80fd\u4f1a\u51fa\u73b0\u5f02\u5e38\u60c5\u51b5\u3002\u901a\u8fc7\u6355\u83b7\u6570\u636e\u5904\u7406\u4e2d\u7684\u5f02\u5e38\uff0c\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u6570\u636e\u95ee\u9898\u5d29\u6e83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_list = [<\/p>\n<p>    &quot;Data for file 1&quot;,<\/p>\n<p>    &quot;Data for file 2&quot;,<\/p>\n<p>    &quot;Data for file 3&quot;<\/p>\n<p>]<\/p>\n<p>for i, data in enumerate(data_list):<\/p>\n<p>    filename = f&quot;file_{i+1}.txt&quot;<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, &#39;w&#39;) as file:<\/p>\n<p>            file.write(data)<\/p>\n<p>    except IOError as e:<\/p>\n<p>        print(f&quot;An error occurred while writing to {filename}: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u6587\u4ef6\u5199\u5165\u6548\u7387<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u65f6\uff0c\u4f18\u5316\u6587\u4ef6\u5199\u5165\u6548\u7387\u975e\u5e38\u91cd\u8981\u3002\u901a\u8fc7\u5408\u7406\u7684\u7b56\u7565\uff0c\u53ef\u4ee5\u5927\u5e45\u63d0\u9ad8\u6587\u4ef6\u5199\u5165\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u6279\u91cf\u5199\u5165<\/h4>\n<\/p>\n<p><p>\u6279\u91cf\u5199\u5165\u662f\u4e00\u79cd\u6709\u6548\u7684\u4f18\u5316\u7b56\u7565\u3002\u901a\u8fc7\u4e00\u6b21\u6027\u5199\u5165\u591a\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u51cf\u5c11\u6587\u4ef6\u64cd\u4f5c\u7684\u5f00\u9500\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_list = [<\/p>\n<p>    &quot;Data for file 1&quot;,<\/p>\n<p>    &quot;Data for file 2&quot;,<\/p>\n<p>    &quot;Data for file 3&quot;<\/p>\n<p>]<\/p>\n<p>buffer = []<\/p>\n<p>for i, data in enumerate(data_list):<\/p>\n<p>    buffer.append((f&quot;file_{i+1}.txt&quot;, data))<\/p>\n<p>for filename, data in buffer:<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u4f7f\u7528\u591a\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u63d0\u9ad8\u5199\u5165\u6548\u7387\u3002\u901a\u8fc7\u5e76\u884c\u5904\u7406\uff0c\u53ef\u4ee5\u5927\u5e45\u51cf\u5c11\u6587\u4ef6\u64cd\u4f5c\u7684\u65f6\u95f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>data_list = [<\/p>\n<p>    &quot;Data for file 1&quot;,<\/p>\n<p>    &quot;Data for file 2&quot;,<\/p>\n<p>    &quot;Data for file 3&quot;<\/p>\n<p>]<\/p>\n<p>def write_file(filename, data):<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<p>threads = []<\/p>\n<p>for i, data in enumerate(data_list):<\/p>\n<p>    filename = f&quot;file_{i+1}.txt&quot;<\/p>\n<p>    thread = threading.Thread(target=write_file, args=(filename, data))<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4f18\u5316\u7b56\u7565\uff0c\u53ef\u4ee5\u5927\u5e45\u63d0\u9ad8\u6587\u4ef6\u5199\u5165\u7684\u6548\u7387\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u65f6\uff0c\u6548\u679c\u5c24\u4e3a\u663e\u8457\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6279\u91cf\u5199\u5165\u591a\u4e2atxt\u6587\u4ef6\u7684\u573a\u666f\u975e\u5e38\u5e7f\u6cdb\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>6.1 \u6570\u636e\u5907\u4efd<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5907\u4efd\u8fc7\u7a0b\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5c06\u5927\u91cf\u6570\u636e\u5b58\u50a8\u5230\u591a\u4e2a\u6587\u4ef6\u4e2d\u3002\u901a\u8fc7\u6279\u91cf\u5199\u5165\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u6570\u636e\u5907\u4efd\u4efb\u52a1\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>data_list = [<\/p>\n<p>    &quot;Backup data for file 1&quot;,<\/p>\n<p>    &quot;Backup data for file 2&quot;,<\/p>\n<p>    &quot;Backup data for file 3&quot;<\/p>\n<p>]<\/p>\n<p>backup_dir = &quot;backup&quot;<\/p>\n<p>if not os.path.exists(backup_dir):<\/p>\n<p>    os.makedirs(backup_dir)<\/p>\n<p>for i, data in enumerate(data_list):<\/p>\n<p>    filename = os.path.join(backup_dir, f&quot;backup_{i+1}.txt&quot;)<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2 \u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u65e5\u5fd7\u8bb0\u5f55\u8fc7\u7a0b\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5c06\u65e5\u5fd7\u4fe1\u606f\u5b58\u50a8\u5230\u591a\u4e2a\u6587\u4ef6\u4e2d\u3002\u901a\u8fc7\u6279\u91cf\u5199\u5165\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u65e5\u5fd7\u8bb0\u5f55\u4efb\u52a1\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>log_messages = [<\/p>\n<p>    &quot;Log message 1&quot;,<\/p>\n<p>    &quot;Log message 2&quot;,<\/p>\n<p>    &quot;Log message 3&quot;<\/p>\n<p>]<\/p>\n<p>log_dir = &quot;logs&quot;<\/p>\n<p>if not os.path.exists(log_dir):<\/p>\n<p>    os.makedirs(log_dir)<\/p>\n<p>for i, message in enumerate(log_messages):<\/p>\n<p>    filename = os.path.join(log_dir, f&quot;log_{i+1}_{datetime.datetime.now().strftime(&#39;%Y%m%d%H%M%S&#39;)}.txt&quot;)<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.3 \u62a5\u544a\u751f\u6210<\/h4>\n<\/p>\n<p><p>\u5728\u62a5\u544a\u751f\u6210\u8fc7\u7a0b\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5c06\u62a5\u544a\u5185\u5bb9\u5b58\u50a8\u5230\u591a\u4e2a\u6587\u4ef6\u4e2d\u3002\u901a\u8fc7\u6279\u91cf\u5199\u5165\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u62a5\u544a\u751f\u6210\u4efb\u52a1\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">report_data = [<\/p>\n<p>    &quot;Report data for file 1&quot;,<\/p>\n<p>    &quot;Report data for file 2&quot;,<\/p>\n<p>    &quot;Report data for file 3&quot;<\/p>\n<p>]<\/p>\n<p>report_dir = &quot;reports&quot;<\/p>\n<p>if not os.path.exists(report_dir):<\/p>\n<p>    os.makedirs(report_dir)<\/p>\n<p>for i, data in enumerate(report_data):<\/p>\n<p>    filename = os.path.join(report_dir, f&quot;report_{i+1}.txt&quot;)<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0cPython\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u591a\u4e2atxt\u6587\u4ef6\u7684\u5199\u5165\u64cd\u4f5c\u3002\u65e0\u8bba\u662f<strong>\u6570\u636e\u51c6\u5907\u4e0e\u7ba1\u7406\u3001\u6587\u4ef6\u64cd\u4f5c\u57fa\u7840\u3001\u4f7f\u7528\u5faa\u73af\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3001\u4f18\u5316\u6587\u4ef6\u5199\u5165\u6548\u7387<\/strong>\uff0c\u8fd8\u662f<strong>\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/strong>\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u5408\u7406\u7684\u7b56\u7565\u548c\u65b9\u6cd5\uff0c\u8fbe\u5230\u9ad8\u6548\u3001\u7a33\u5b9a\u7684\u6548\u679c\u3002\u5e0c\u671b\u672c\u6587\u80fd\u4e3a\u4f60\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u63d0\u4f9b\u6709\u7528\u7684\u6307\u5bfc\u548c\u53c2\u8003\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6279\u91cf\u521b\u5efa\u591a\u4e2aTXT\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u6587\u4ef6\u64cd\u4f5c\u6765\u6279\u91cf\u521b\u5efa\u591a\u4e2aTXT\u6587\u4ef6\u3002\u9996\u5148\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6587\u4ef6\u540d\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u5728\u5faa\u73af\u4e2d\u521b\u5efa\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">file_names = [&quot;file1.txt&quot;, &quot;file2.txt&quot;, &quot;file3.txt&quot;]\nfor name in file_names:\n    with open(name, &#39;w&#39;) as file:\n        file.write(&quot;\u8fd9\u662f\u6587\u4ef6\u5185\u5bb9&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06\u6570\u636e\u5199\u5165\u591a\u4e2aTXT\u6587\u4ef6\u800c\u4e0d\u662f\u521b\u5efa\u7a7a\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u5728\u521b\u5efa\u6587\u4ef6\u65f6\u76f4\u63a5\u5199\u5165\u6570\u636e\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u5728\u5faa\u73af\u4e2d\u5c06\u6bcf\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u5199\u5165\u5bf9\u5e94\u7684\u6587\u4ef6\u4e2d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">data = [&quot;\u5185\u5bb91&quot;, &quot;\u5185\u5bb92&quot;, &quot;\u5185\u5bb93&quot;]\nfor i in range(len(data)):\n    with open(f&quot;file{i+1}.txt&quot;, &#39;w&#39;) as file:\n        file.write(data[i])\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4ece\u4e00\u4e2aTXT\u6587\u4ef6\u8bfb\u53d6\u5185\u5bb9\u5e76\u5c06\u5176\u5b58\u5165\u591a\u4e2aTXT\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u4e00\u4e2a\u6e90\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u7136\u540e\u5c06\u5176\u5206\u5272\u5e76\u5199\u5165\u591a\u4e2a\u6587\u4ef6\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6e90\u6587\u4ef6\u7684\u6240\u6709\u884c\uff0c\u5e76\u5728\u5faa\u73af\u4e2d\u5c06\u5176\u5199\u5165\u4e0d\u540c\u7684TXT\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&quot;source.txt&quot;, &#39;r&#39;) as source_file:\n    lines = source_file.readlines()\n    \nfor i, line in enumerate(lines):\n    with open(f&quot;output_file_{i+1}.txt&quot;, &#39;w&#39;) as output_file:\n        output_file.write(line)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u5e2e\u52a9\u60a8\u5728Python\u4e2d\u521b\u5efa\u548c\u7ba1\u7406\u591a\u4e2aTXT\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e00\u6b21\u5b58\u5165\u591a\u4e2atxt\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u521b\u5efa\u6570\u636e\u5217\u8868\u3001\u4f7f\u7528\u5faa\u73af\u3001\u6253\u5f00\u6587\u4ef6\u3001\u5199\u5165\u6570\u636e\u3002\u5176\u4e2d\uff0c\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1121997,"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\/1121992"}],"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=1121992"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121992\/revisions"}],"predecessor-version":[{"id":1122000,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121992\/revisions\/1122000"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1121997"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1121992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1121992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1121992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}