{"id":1051904,"date":"2024-12-31T14:20:25","date_gmt":"2024-12-31T06:20:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1051904.html"},"modified":"2024-12-31T14:20:28","modified_gmt":"2024-12-31T06:20:28","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1051904.html","title":{"rendered":"python\u5982\u4f55\u5199\u6587\u672c\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/88629ce9-8d44-4f21-a52f-ed628633dbba.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5199\u6587\u672c\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u5199\u6587\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684 open() \u51fd\u6570\u3001\u5199\u5165\u6587\u672c\u5185\u5bb9\u3001\u5173\u95ed\u6587\u4ef6\u7b49\u6b65\u9aa4\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u4f7f\u7528 with open() \u8bed\u53e5\u3001\u533a\u5206\u5199\u5165\u6a21\u5f0f\u3001\u5904\u7406\u6587\u4ef6\u5f02\u5e38\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684 open() \u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684 open() \u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u7684\u8def\u5f84\u548c\u6253\u5f00\u6a21\u5f0f\u3002\u5e38\u89c1\u7684\u6a21\u5f0f\u6709\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>&#39;w&#39;<\/code>\uff1a\u5199\u5165\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u4f1a\u8986\u76d6\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u521b\u5efa\u65b0\u6587\u4ef6\u3002<\/li>\n<li><code>&#39;a&#39;<\/code>\uff1a\u8ffd\u52a0\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u4f1a\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\uff1b\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u521b\u5efa\u65b0\u6587\u4ef6\u3002<\/li>\n<li><code>&#39;r&#39;<\/code>\uff1a\u8bfb\u53d6\u6a21\u5f0f\u3002\u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/li>\n<li><code>&#39;b&#39;<\/code>\uff1a\u4e8c\u8fdb\u5236\u6a21\u5f0f\u3002\u7528\u4e8e\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\uff08\u5982\u56fe\u7247\u7b49\uff09\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">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>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a <code>example.txt<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u5199\u5165\u4e86\u5b57\u7b26\u4e32 <code>Hello, world!<\/code>\u3002\u6700\u540e\uff0c\u6211\u4eec\u5173\u95ed\u4e86\u6587\u4ef6\u3002<strong>\u5173\u95ed\u6587\u4ef6\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u786e\u4fdd\u6240\u6709\u6570\u636e\u90fd\u88ab\u5199\u5165\u6587\u4ef6\u5e76\u91ca\u653e\u6587\u4ef6\u8d44\u6e90\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 with open() \u8bed\u53e5<\/h3>\n<\/p>\n<p><p><code>with open()<\/code> \u8bed\u53e5\u662f\u4e00\u79cd\u66f4\u4f18\u96c5\u4e14\u63a8\u8350\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\uff0c\u56e0\u4e3a\u5b83\u4f1a\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u7684\u5173\u95ed\uff0c\u5373\u4f7f\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u3002\u4f7f\u7528 <code>with open()<\/code> \u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u5e76\u786e\u4fdd\u6587\u4ef6\u603b\u662f\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/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(&#39;Hello, world!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6587\u4ef6\u4f1a\u5728 <code>with<\/code> \u4ee3\u7801\u5757\u7ed3\u675f\u65f6\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u533a\u5206\u5199\u5165\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u5199\u5165\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f <code>&#39;a&#39;<\/code> \u6765\u5411\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\u3002<\/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;\\nNew line added.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728 <code>example.txt<\/code> \u6587\u4ef6\u7684\u672b\u5c3e\u6dfb\u52a0\u4e86\u65b0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u6587\u4ef6\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u5404\u79cd\u5f02\u5e38\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u65e0\u6743\u9650\u5199\u5165\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>try-except<\/code> \u8bed\u53e5\u6765\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/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(&#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>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u53d1\u751f\u6587\u4ef6\u64cd\u4f5c\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7\u5e76\u6253\u5370\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5199\u5165\u591a\u884c\u6587\u672c<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>writelines()<\/code> \u65b9\u6cd5\u6765\u5199\u5165\u591a\u884c\u6587\u672c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>writelines()<\/code> \u65b9\u6cd5\u4e0d\u4f1a\u81ea\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\uff0c\u9700\u8981\u624b\u52a8\u6dfb\u52a0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&#39;First line\\n&#39;, &#39;Second line\\n&#39;, &#39;Third line\\n&#39;]<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5199\u5165\u683c\u5f0f\u5316\u6587\u672c<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff08\u5982 f-string\u3001format()\uff09\u6765\u5199\u5165\u683c\u5f0f\u5316\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &#39;John&#39;<\/p>\n<p>age = 30<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(f&#39;Name: {name}\\nAge: {age}\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 f-string \u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5e76\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u5199\u5165\u6587\u4ef6\u540e\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u6765\u9a8c\u8bc1\u5199\u5165\u662f\u5426\u6210\u529f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>&#39;r&#39;<\/code> \u6a21\u5f0f\u6765\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u5e76\u6253\u5370\u4e86 <code>example.txt<\/code> \u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u6587\u672c\u6587\u4ef6\u7684\u5199\u5165\u548c\u8bfb\u53d6\u3002\u4f7f\u7528\u5185\u7f6e\u7684 open() \u51fd\u6570\u548c with open() \u8bed\u53e5\u662f\u5e38\u89c1\u4e14\u63a8\u8350\u7684\u65b9\u5f0f\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5199\u5165\u6a21\u5f0f\uff08\u5982 <code>&#39;w&#39;<\/code>\u3001<code>&#39;a&#39;<\/code>\uff09\u3001\u5904\u7406\u6587\u4ef6\u5f02\u5e38\u3001\u5199\u5165\u591a\u884c\u6216\u683c\u5f0f\u5316\u6587\u672c\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8bb2\u89e3\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528 Python \u6765\u5199\u6587\u672c\u6587\u4ef6\uff0c\u5e76\u638c\u63e1\u4e86\u4e00\u4e9b\u5b9e\u7528\u7684\u6280\u5de7\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5f00\u548c\u521b\u5efa\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u6253\u5f00\u6216\u521b\u5efa\u6587\u672c\u6587\u4ef6\u3002\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\uff08\u5982&quot;w&quot;\u8868\u793a\u5199\u5165\u6a21\u5f0f\uff0c&quot;a&quot;\u8868\u793a\u8ffd\u52a0\u6a21\u5f0f\uff09\uff0c\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<code>open(&quot;example.txt&quot;, &quot;w&quot;)<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example.txt<\/code>\u7684\u65b0\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5219\u4f1a\u8986\u76d6\u5176\u5185\u5bb9\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5199\u5165\u6587\u672c\u6587\u4ef6\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u5199\u5165\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u662f\u6700\u4f73\u5b9e\u8df5\u3002\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u786e\u4fdd\u6587\u4ef6\u5728\u5199\u5165\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u56e0\u672a\u5173\u95ed\u6587\u4ef6\u800c\u5bfc\u81f4\u7684\u6f5c\u5728\u95ee\u9898\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;) as file:\n    file.write(&quot;Hello, World!&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u5199\u5165\u540e\u5b89\u5168\u5173\u95ed\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8ffd\u52a0\u5185\u5bb9\u5230\u73b0\u6709\u7684\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5411\u73b0\u6709\u6587\u4ef6\u6dfb\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u76d6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\uff08&quot;a&quot;\uff09\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;a&quot;) as file:\n    file.write(&quot;\\nThis is an additional line.&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u628a\u65b0\u5185\u5bb9\u6dfb\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u800c\u4e0d\u4f1a\u5220\u9664\u539f\u6709\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5199\u6587\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684 open() \u51fd\u6570\u3001\u5199\u5165\u6587\u672c\u5185\u5bb9\u3001\u5173\u95ed\u6587\u4ef6\u7b49\u6b65\u9aa4\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1051910,"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\/1051904"}],"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=1051904"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051904\/revisions"}],"predecessor-version":[{"id":1051913,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051904\/revisions\/1051913"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1051910"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1051904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1051904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1051904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}