{"id":979291,"date":"2024-12-27T06:45:08","date_gmt":"2024-12-26T22:45:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/979291.html"},"modified":"2024-12-27T06:45:10","modified_gmt":"2024-12-26T22:45:10","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bc%96%e8%be%91%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/979291.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u7f16\u8f91\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205209\/575f4674-fa35-471f-998d-7a5a1581e0f5.webp\" alt=\"python\u4e2d\u5982\u4f55\u7f16\u8f91\u6587\u4ef6\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u7f16\u8f91\u6587\u4ef6\u4e3b\u8981\u6d89\u53ca<strong>\u6253\u5f00\u6587\u4ef6\u3001\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\u3001\u5173\u95ed\u6587\u4ef6<\/strong>\u7b49\u64cd\u4f5c\u3002Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u4f7f\u5f97\u6587\u4ef6\u7f16\u8f91\u53d8\u5f97\u76f4\u89c2\u4e14\u9ad8\u6548\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6587\u4ef6\u7f16\u8f91\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u7528\u7684\u6280\u5de7\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6253\u5f00\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8981\u7f16\u8f91\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u6253\u5f00\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u3002<code>open()<\/code>\u51fd\u6570\u6709\u4e0d\u540c\u7684\u6a21\u5f0f\u6765\u6307\u5b9a\u6587\u4ef6\u7684\u6253\u5f00\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>&#39;r&#39;<\/code>\uff1a\u53ea\u8bfb\u6a21\u5f0f\uff08\u9ed8\u8ba4\uff09\u3002<\/li>\n<li><code>&#39;w&#39;<\/code>\uff1a\u5199\u5165\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u4f1a\u5148\u6e05\u7a7a\u6587\u4ef6\u3002<\/li>\n<li><code>&#39;a&#39;<\/code>\uff1a\u8ffd\u52a0\u6a21\u5f0f\u3002\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\u3002<\/li>\n<li><code>&#39;r+&#39;<\/code>\uff1a\u8bfb\u5199\u6a21\u5f0f\u3002<\/li>\n<li><code>&#39;w+&#39;<\/code>\uff1a\u8bfb\u5199\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u4f1a\u5148\u6e05\u7a7a\u6587\u4ef6\u3002<\/li>\n<li><code>&#39;a+&#39;<\/code>\uff1a\u8bfb\u5199\u6a21\u5f0f\u3002\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)  # \u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1. <code>read()<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>read()<\/code>\u65b9\u6cd5\u7528\u4e8e\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">content = file.read()<\/p>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. <code>readline()<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>readline()<\/code>\u65b9\u6cd5\u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6\u7684\u4e00\u884c\uff0c\u9002\u5408\u9010\u884c\u5904\u7406\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">line = file.readline()<\/p>\n<p>while line:<\/p>\n<p>    print(line, end=&#39;&#39;)<\/p>\n<p>    line = file.readline()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. <code>readlines()<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>readlines()<\/code>\u65b9\u6cd5\u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u884c\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a\u5217\u8868\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = file.readlines()<\/p>\n<p>for line in lines:<\/p>\n<p>    print(line, end=&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5199\u5165\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u8981\u5199\u5165\u6587\u4ef6\uff0c\u9700\u8981\u5c06\u6587\u4ef6\u4ee5\u5199\u6a21\u5f0f\uff08<code>&#39;w&#39;<\/code>, <code>&#39;a&#39;<\/code>, <code>&#39;r+&#39;<\/code>, <code>&#39;w+&#39;<\/code>, <code>&#39;a+&#39;<\/code>\uff09\u6253\u5f00\u3002<\/p>\n<\/p>\n<p><h4>1. <code>write()<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>write()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\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(&quot;This is a new line.\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. <code>writelines()<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>writelines()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&quot;Line 1\\n&quot;, &quot;Line 2\\n&quot;, &quot;Line 3\\n&quot;]<\/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>\u56db\u3001\u5173\u95ed\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6587\u4ef6\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u5e94\u8be5\u53ca\u65f6\u5173\u95ed\u6587\u4ef6\u3002\u5173\u95ed\u6587\u4ef6\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90\uff0c\u5e76\u786e\u4fdd\u6570\u636e\u5199\u5165\u78c1\u76d8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u907f\u514d\u624b\u52a8\u8c03\u7528<code>close()<\/code>\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><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6587\u4ef6\u7f16\u8f91\u7684\u5b9e\u7528\u6280\u5de7<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>Python\u7684<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u63a8\u8350\u4f7f\u7528\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><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5904\u7406\u5927\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u9010\u884c\u8bfb\u53d6\u548c\u5904\u7406\u66f4\u52a0\u9ad8\u6548\uff0c\u907f\u514d\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        process(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6587\u4ef6\u5907\u4efd<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u8f91\u6587\u4ef6\u524d\u5907\u4efd\u539f\u59cb\u6587\u4ef6\uff0c\u4ee5\u9632\u6570\u636e\u4e22\u5931\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>shutil.copy(&#39;example.txt&#39;, &#39;example_backup.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u4f8b\uff1a\u66ff\u6362\u6587\u4ef6\u4e2d\u7684\u6587\u672c<\/h3>\n<\/p>\n<p><p>\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u66ff\u6362\u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u6587\u4ef6\u4e2d\u67e5\u627e\u5e76\u66ff\u6362\u7279\u5b9a\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def replace_text_in_file(file_path, old_text, new_text):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>    content = content.replace(old_text, new_text)<\/p>\n<p>    with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>        file.write(content)<\/p>\n<p>replace_text_in_file(&#39;example.txt&#39;, &#39;old&#39;, &#39;new&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u6587\u4ef6\u7f16\u8f91\u65b9\u6cd5\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u3001\u53ef\u9760\u7684\u6587\u4ef6\u7f16\u8f91\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6587\u672c\u66ff\u6362\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u5904\u7406\uff0cPython\u7684\u6587\u4ef6\u7f16\u8f91\u529f\u80fd\u90fd\u80fd\u6ee1\u8db3\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u548c\u6587\u4ef6\u5907\u4efd\u7b49\u6280\u5de7\uff0c\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6253\u5f00\u5e76\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\u3002\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u548c\u6a21\u5f0f\uff08\u5982\u8bfb\u53d6\u6a21\u5f0f&#39;r&#39;\uff09\uff0c\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u7528\u54ea\u4e9b\u65b9\u5f0f\u5199\u5165\u6587\u4ef6\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u591a\u79cd\u65b9\u6cd5\u6765\u5199\u5165\u6587\u4ef6\u3002\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u4ee5\u5199\u5165\u6a21\u5f0f\uff08&#39;w&#39;\uff09\u6216\u8ffd\u52a0\u6a21\u5f0f\uff08&#39;a&#39;\uff09\u6253\u5f00\u6587\u4ef6\u3002\u5199\u5165\u6a21\u5f0f\u4f1a\u8986\u76d6\u539f\u6709\u5185\u5bb9\uff0c\u800c\u8ffd\u52a0\u6a21\u5f0f\u5219\u4f1a\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u65b0\u5185\u5bb9\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;a&#39;) as file:\n    file.write(&#39;\u65b0\u7684\u4e00\u884c\\n&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7b80\u5355\u660e\u4e86\uff0c\u9002\u5408\u7528\u4e8e\u5199\u5165\u6587\u672c\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6587\u4ef6\u5199\u5165\u65f6\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u6bd4\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6743\u9650\u4e0d\u8db3\u3002\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u53ef\u4ee5\u6709\u6548\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    with open(&#39;example.txt&#39;, &#39;w&#39;) as file:\n        file.write(&#39;\u5199\u5165\u5185\u5bb9&#39;)\nexcept IOError as e:\n    print(f&#39;\u6587\u4ef6\u64cd\u4f5c\u5931\u8d25: {e}&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u80fd\u591f\u83b7\u5f97\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u4ece\u800c\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7f16\u8f91\u6587\u4ef6\u4e3b\u8981\u6d89\u53ca\u6253\u5f00\u6587\u4ef6\u3001\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\u3001\u5173\u95ed\u6587\u4ef6\u7b49\u64cd\u4f5c\u3002Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":979292,"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\/979291"}],"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=979291"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979291\/revisions"}],"predecessor-version":[{"id":979294,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979291\/revisions\/979294"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/979292"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=979291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=979291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=979291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}