{"id":1101731,"date":"2025-01-08T15:52:31","date_gmt":"2025-01-08T07:52:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101731.html"},"modified":"2025-01-08T15:52:37","modified_gmt":"2025-01-08T07:52:37","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aatxt%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101731.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u521b\u5efa\u4e00\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\/25064350\/4d1abd9d-1da9-49a8-ae4f-01e619fbd0e8.webp\" alt=\"python\u4e2d\u5982\u4f55\u521b\u5efa\u4e00\u4e2atxt\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2atxt\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570open()\u3001\u4f7f\u7528with\u8bed\u53e5\u7ba1\u7406\u6587\u4ef6\u4e0a\u4e0b\u6587\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982pathlib\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570open()\u548cwith\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u548c\u6700\u63a8\u8350\u7684\u65b9\u5f0f\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2atxt\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528open()\u51fd\u6570\u521b\u5efatxt\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0copen()\u51fd\u6570\u662f\u6700\u57fa\u672c\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002\u5f53\u6587\u4ef6\u4e0d\u5b58\u5728\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6a21\u5f0f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684txt\u6587\u4ef6\u3002\u5177\u4f53\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528open()\u51fd\u6570\u521b\u5efatxt\u6587\u4ef6<\/p>\n<p>file = open(&#39;example.txt&#39;, &#39;w&#39;)<\/p>\n<p>file.write(&#39;Hello, world!&#39;)<\/p>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>open(&#39;example.txt&#39;, &#39;w&#39;)<\/code>\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u4f1a\u88ab\u521b\u5efa\uff0c\u6a21\u5f0f<code>&#39;w&#39;<\/code>\u8868\u793a\u5199\u5165\u6a21\u5f0f\u3002\u7136\u540e\u4f7f\u7528<code>file.write(&#39;Hello, world!&#39;)<\/code>\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u5185\u5bb9\uff0c\u6700\u540e\u4f7f\u7528<code>file.close()<\/code>\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528with\u8bed\u53e5\u7ba1\u7406\u6587\u4ef6\u4e0a\u4e0b\u6587<\/p>\n<\/p>\n<p><p>\u4f7f\u7528with\u8bed\u53e5\u53ef\u4ee5\u66f4\u52a0\u7b80\u6d01\u548c\u5b89\u5168\u5730\u7ba1\u7406\u6587\u4ef6\u64cd\u4f5c\uff0c\u5b83\u4f1a\u5728\u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6bd5\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u907f\u514d\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u7684\u95ee\u9898\u3002\u5177\u4f53\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528with\u8bed\u53e5\u521b\u5efatxt\u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, world!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>with open(&#39;example.txt&#39;, &#39;w&#39;) as file<\/code>\u7528\u4e8e\u521b\u5efa\u5e76\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165\u64cd\u4f5c\uff0c<code>with<\/code>\u8bed\u53e5\u7ed3\u675f\u540e\u4f1a\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528pathlib\u5e93\u521b\u5efatxt\u6587\u4ef6<\/p>\n<\/p>\n<p><p>pathlib\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002\u4f7f\u7528pathlib\u5e93\u53ef\u4ee5\u66f4\u52a0\u65b9\u4fbf\u5730\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u3002\u5177\u4f53\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u4f7f\u7528pathlib\u5e93\u521b\u5efatxt\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = Path(&#39;example.txt&#39;)<\/p>\n<p>file_path.write_text(&#39;Hello, world!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>Path(&#39;example.txt&#39;)<\/code>\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u8def\u5f84\u5bf9\u8c61\uff0c<code>file_path.write_text(&#39;Hello, world!&#39;)<\/code>\u7528\u4e8e\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u5185\u5bb9\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u4f1a\u81ea\u52a8\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9644\u52a0\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u9762\u63d0\u5230\u7684\u4e09\u79cd\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u9644\u52a0\u5185\u5bb9\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u6587\u4ef6\u6a21\u5f0f\uff1a\u5728\u4f7f\u7528open()\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u6a21\u5f0f\uff0c\u5982<code>&#39;w&#39;<\/code>\uff08\u5199\u5165\u6a21\u5f0f\uff09\u3001<code>&#39;a&#39;<\/code>\uff08\u8ffd\u52a0\u6a21\u5f0f\uff09\u3001<code>&#39;r&#39;<\/code>\uff08\u8bfb\u53d6\u6a21\u5f0f\uff09\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u6a21\u5f0f\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u6587\u4ef6\u7f16\u7801\uff1a\u5728\u5904\u7406\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff0c\u5982UTF-8\u3002\u53ef\u4ee5\u901a\u8fc7<code>encoding<\/code>\u53c2\u6570\u6307\u5b9a\u7f16\u7801\u65b9\u5f0f\uff0c\u5982<code>open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;)<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u5904\u7406\uff1a\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/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>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u521b\u5efatxt\u6587\u4ef6\uff0c\u5e76\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u548c\u6587\u4ef6\u7f16\u7801\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u4f7f\u7528open()\u51fd\u6570\u521b\u5efatxt\u6587\u4ef6<\/strong><\/h2>\n<p>try:<\/p>\n<p>    file = open(&#39;example_open.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;)<\/p>\n<p>    file.write(&#39;Hello, world!&#39;)<\/p>\n<p>    file.close()<\/p>\n<p>except IOError as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528with\u8bed\u53e5\u521b\u5efatxt\u6587\u4ef6<\/strong><\/h2>\n<p>try:<\/p>\n<p>    with open(&#39;example_with.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/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<h2><strong>\u4f7f\u7528pathlib\u5e93\u521b\u5efatxt\u6587\u4ef6<\/strong><\/h2>\n<p>try:<\/p>\n<p>    file_path = Path(&#39;example_pathlib.txt&#39;)<\/p>\n<p>    file_path.write_text(&#39;Hello, world!&#39;, encoding=&#39;utf-8&#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>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5728Python\u4e2d\u521b\u5efatxt\u6587\u4ef6\u7684\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\uff1a\u4f7f\u7528open()\u51fd\u6570\u3001\u4f7f\u7528with\u8bed\u53e5\u7ba1\u7406\u6587\u4ef6\u4e0a\u4e0b\u6587\u3001\u4f7f\u7528pathlib\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u6587\u4ef6\u6a21\u5f0f\u3001\u6587\u4ef6\u7f16\u7801\u548c\u5f02\u5e38\u5904\u7406\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1Python\u4e2d\u7684\u6587\u4ef6\u64cd\u4f5c\uff0c\u5e76\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684txt\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684txt\u6587\u4ef6\u3002\u53ea\u9700\u5c06\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u4f8b\u5982<code>open(&#39;filename.txt&#39;, &#39;w&#39;)<\/code>\uff0c\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\uff08\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5c06\u4f1a\u88ab\u6e05\u7a7a\uff09\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\uff0c\u6700\u540e\u901a\u8fc7<code>close()<\/code>\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\u4ee5\u786e\u4fdd\u6240\u6709\u6570\u636e\u90fd\u5df2\u4fdd\u5b58\u3002<\/p>\n<p><strong>Python\u4e2d\u521b\u5efatxt\u6587\u4ef6\u65f6\u53ef\u4ee5\u6dfb\u52a0\u54ea\u4e9b\u5185\u5bb9\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u5c06\u4efb\u4f55\u5b57\u7b26\u4e32\u5185\u5bb9\u6dfb\u52a0\u5230txt\u6587\u4ef6\u4e2d\uff0c\u5305\u62ec\u6587\u672c\u3001\u6570\u5b57\u548c\u7b26\u53f7\u3002\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5199\u5165\u5355\u884c\u6587\u672c\uff0c\u4f7f\u7528<code>writelines()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u5199\u5165\u591a\u884c\u6587\u672c\u3002\u786e\u4fdd\u4f7f\u7528\u6362\u884c\u7b26<code>\\n<\/code>\u6765\u5206\u9694\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5txt\u6587\u4ef6\u662f\u5426\u6210\u529f\u521b\u5efa\uff1f<\/strong><br \/>\u521b\u5efatxt\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>path.exists()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6210\u529f\u521b\u5efa\u3002\u9996\u5148\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528<code>os.path.exists(&#39;filename.txt&#39;)<\/code>\uff0c\u5982\u679c\u8fd4\u56de\u503c\u4e3a<code>True<\/code>\uff0c\u5219\u8868\u793a\u6587\u4ef6\u5b58\u5728\u3002\u5982\u679c\u9700\u8981\u67e5\u770b\u6587\u4ef6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u4ee5\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u67e5\u770b\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2atxt\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570open()\u3001\u4f7f\u7528with\u8bed\u53e5\u7ba1\u7406\u6587\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":1101742,"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\/1101731"}],"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=1101731"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101731\/revisions"}],"predecessor-version":[{"id":1101743,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101731\/revisions\/1101743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101742"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}