{"id":1176738,"date":"2025-01-15T17:48:45","date_gmt":"2025-01-15T09:48:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1176738.html"},"modified":"2025-01-15T17:49:03","modified_gmt":"2025-01-15T09:49:03","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e6%b5%8f%e8%a7%88txt%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1176738.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00\u6d4f\u89c8txt\u6587\u6863"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25111829\/73d8e544-7dda-4fa5-90b2-f0523ba4aa76.webp\" alt=\"python\u5982\u4f55\u6253\u5f00\u6d4f\u89c8txt\u6587\u6863\" \/><\/p>\n<p><p> <strong>Python \u6253\u5f00\u6d4f\u89c8txt\u6587\u6863\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6bd4\u5982\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 open()\u3001with\u8bed\u53e5\u3001\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u6216\u9010\u884c\u8bfb\u53d6\u7b49\u3002\u5177\u4f53\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1aopen()\u51fd\u6570\u3001with\u8bed\u53e5\u3001\u9010\u884c\u8bfb\u53d6\u3002<\/strong> \u5176\u4e2d\uff0c<strong>open()\u51fd\u6570<\/strong>\u6700\u4e3a\u5e38\u7528\uff0c\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>open()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u3002<code>open()<\/code>\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u6587\u4ef6\u8def\u5f84\u548c\u6a21\u5f0f\u3002\u5e38\u89c1\u7684\u6a21\u5f0f\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>&#39;r&#39;\uff1a\u53ea\u8bfb\u6a21\u5f0f\uff08\u9ed8\u8ba4\u503c\uff09<\/li>\n<li>&#39;w&#39;\uff1a\u5199\u6a21\u5f0f\uff08\u4f1a\u8986\u76d6\u6587\u4ef6\u5185\u5bb9\uff09<\/li>\n<li>&#39;a&#39;\uff1a\u8ffd\u52a0\u6a21\u5f0f\uff08\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\uff09<\/li>\n<li>&#39;b&#39;\uff1a\u4e8c\u8fdb\u5236\u6a21\u5f0f<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u8bfb\u53d6\u4e00\u4e2atxt\u6587\u4ef6\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>content = file.read()<\/p>\n<p>print(content)<\/p>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a &#39;example.txt&#39; \u7684\u6587\u4ef6\uff0c\u8bfb\u53d6\u5176\u5185\u5bb9\u5e76\u6253\u5370\u51fa\u6765\uff0c\u6700\u540e\u5173\u95ed\u4e86\u6587\u4ef6\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u66f4\u7b80\u6d01\u5730\u5904\u7406\u6587\u4ef6\uff0c\u5e76\u4e14\u4e0d\u7528\u62c5\u5fc3\u5173\u95ed\u6587\u4ef6\u7684\u95ee\u9898\uff1a<\/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\u79cd\u65b9\u5f0f\u4f1a\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u7684\u5173\u95ed\u64cd\u4f5c\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u5b89\u5168\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528 open() \u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>Python \u4e2d\u7684 <code>open()<\/code> \u51fd\u6570\u662f\u6253\u5f00\u6587\u4ef6\u7684\u57fa\u7840\u65b9\u6cd5\u3002\u5b83\u63a5\u53d7\u4e24\u4e2a\u4e3b\u8981\u53c2\u6570\uff1a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u3002\u6a21\u5f0f\u53ef\u4ee5\u662f\u8bfb\u6a21\u5f0f\uff08&#39;r&#39;\uff09\u3001\u5199\u6a21\u5f0f\uff08&#39;w&#39;\uff09\u3001\u8ffd\u52a0\u6a21\u5f0f\uff08&#39;a&#39;\uff09\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>content = file.read()<\/p>\n<p>print(content)<\/p>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>open(&#39;example.txt&#39;, &#39;r&#39;)<\/code> \u6253\u5f00\u4e86\u540d\u4e3a <code>example.txt<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u4ee5\u8bfb\u6a21\u5f0f\u8fdb\u884c\u64cd\u4f5c\u3002<code>file.read()<\/code> \u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u5185\u5bb9\uff0c\u5e76\u5b58\u50a8\u5728 <code>content<\/code> \u53d8\u91cf\u4e2d\u3002\u6700\u540e\uff0c<code>file.close()<\/code> \u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u9010\u884c\u8bfb\u53d6<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u66f4\u4e3a\u5408\u9002\uff0c\u7279\u522b\u662f\u5f53\u6587\u4ef6\u8f83\u5927\u65f6\u3002\u9010\u884c\u8bfb\u53d6\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>readline()<\/code> \u6216 <code>readlines()<\/code> \u65b9\u6cd5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>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>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 <code>readline()<\/code> \u65b9\u6cd5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u901a\u8fc7\u5faa\u73af\u8bfb\u53d6\u6bcf\u4e00\u884c\uff0c\u76f4\u5230\u6587\u4ef6\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 <code>readlines()<\/code> \u65b9\u6cd5\uff0c\u5b83\u5c06\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>lines = file.readlines()<\/p>\n<p>for line in lines:<\/p>\n<p>    print(line, end=&#39;&#39;)<\/p>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 with \u8bed\u53e5<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>with<\/code> \u8bed\u53e5\u662f\u5904\u7406\u6587\u4ef6\u7684\u63a8\u8350\u65b9\u6cd5\u3002\u5b83\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u8fd8\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u7684\u5173\u95ed\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u7684\u793a\u4f8b\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>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/code> \u6253\u5f00\u6587\u4ef6\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9 <code>file<\/code> \u53d8\u91cf\u3002<code>with<\/code> \u8bed\u53e5\u5757\u7ed3\u675f\u540e\uff0c\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u9010\u884c\u8bfb\u53d6<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\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>    for line in file:<\/p>\n<p>        print(line, end=&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528\u4e86\u6587\u4ef6\u5bf9\u8c61\u7684\u8fed\u4ee3\u5668\u7279\u6027\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8bfb\u53d6\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1\u3001\u9010\u5757\u8bfb\u53d6<\/h4>\n<\/p>\n<p><p>\u5f53\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u9010\u5757\u8bfb\u53d6\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5757\u5927\u5c0f\uff08\u4f8b\u5982\u6bcf\u6b21\u8bfb\u53d6 1024 \u5b57\u8282\uff09\u6765\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\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>    while True:<\/p>\n<p>        chunk = file.read(1024)<\/p>\n<p>        if not chunk:<\/p>\n<p>            break<\/p>\n<p>        print(chunk, end=&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u9010\u5757\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<code>file.read(1024)<\/code> \u6bcf\u6b21\u8bfb\u53d6 1024 \u5b57\u8282\uff0c\u76f4\u5230\u6587\u4ef6\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u9010\u884c\u8bfb\u53d6<\/h4>\n<\/p>\n<p><p>\u9010\u884c\u8bfb\u53d6\u4e5f\u662f\u5904\u7406\u5927\u6587\u4ef6\u7684\u6709\u6548\u65b9\u6cd5\u3002\u901a\u8fc7\u9010\u884c\u8bfb\u53d6\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\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>        print(line, end=&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 <code>for line in file<\/code> \u8fed\u4ee3\u5668\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u9002\u7528\u4e8e\u5927\u6587\u4ef6\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5199\u5165\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u5199\u5165<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u8bfb\u53d6\u6587\u4ef6\uff0cPython \u7684 <code>open()<\/code> \u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5199\u5165\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5199\u6a21\u5f0f\uff08&#39;w&#39;\uff09\u6216\u8ffd\u52a0\u6a21\u5f0f\uff08&#39;a&#39;\uff09\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, world!\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/code> \u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 <code>file.write()<\/code> \u65b9\u6cd5\u5199\u5165\u5185\u5bb9\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u4f1a\u8986\u76d6\u539f\u6709\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8ffd\u52a0\u5199\u5165<\/h4>\n<\/p>\n<p><p>\u8ffd\u52a0\u6a21\u5f0f\uff08&#39;a&#39;\uff09\u7528\u4e8e\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\uff0c\u800c\u4e0d\u8986\u76d6\u539f\u6709\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&#39;Append this line.\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>with open(&#39;output.txt&#39;, &#39;a&#39;) as file:<\/code> \u4ee5\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 <code>file.write()<\/code> \u65b9\u6cd5\u8ffd\u52a0\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6587\u4ef6\u6a21\u5f0f<\/h3>\n<\/p>\n<p><h4>1\u3001\u6587\u672c\u6a21\u5f0f\u4e0e\u4e8c\u8fdb\u5236\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u8bfb\u3001\u5199\u3001\u8ffd\u52a0\u6a21\u5f0f\uff0c\u6587\u4ef6\u8fd8\u53ef\u4ee5\u4ee5\u6587\u672c\u6a21\u5f0f\uff08\u9ed8\u8ba4\uff09\u6216\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u3002\u6587\u672c\u6a21\u5f0f\u9002\u7528\u4e8e\u5904\u7406\u6587\u672c\u6587\u4ef6\uff0c\u800c\u4e8c\u8fdb\u5236\u6a21\u5f0f\u9002\u7528\u4e8e\u5904\u7406\u975e\u6587\u672c\u6587\u4ef6\uff08\u5982\u56fe\u50cf\u3001\u97f3\u9891\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6587\u672c\u6a21\u5f0f<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<h2><strong>\u4e8c\u8fdb\u5236\u6a21\u5f0f<\/strong><\/h2>\n<p>with open(&#39;image.png&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>&#39;r&#39;<\/code> \u8868\u793a\u4ee5\u6587\u672c\u6a21\u5f0f\u8bfb\u53d6\u6587\u4ef6\uff0c\u800c <code>&#39;rb&#39;<\/code> \u8868\u793a\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8bfb\u5199\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u8bfb\u5199\u6a21\u5f0f\uff08&#39;r+&#39;\uff09\u5141\u8bb8\u540c\u65f6\u8bfb\u53d6\u548c\u5199\u5165\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>    file.write(&#39;Additional content.\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>with open(&#39;example.txt&#39;, &#39;r+&#39;) as file:<\/code> \u4ee5\u8bfb\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002<code>file.read()<\/code> \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c<code>file.write()<\/code> \u5199\u5165\u65b0\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6587\u4ef6\u6307\u9488\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e<\/h4>\n<\/p>\n<p><p>\u6587\u4ef6\u6307\u9488\u51b3\u5b9a\u4e86\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6216\u5199\u5165\u7684\u4f4d\u7f6e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>tell()<\/code> \u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e\uff0c\u4f7f\u7528 <code>seek()<\/code> \u65b9\u6cd5\u79fb\u52a8\u6587\u4ef6\u6307\u9488\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>    print(file.tell())  # \u8f93\u51fa\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e<\/p>\n<p>    content = file.read(5)<\/p>\n<p>    print(content)<\/p>\n<p>    file.seek(0)  # \u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u5f00\u5934<\/p>\n<p>    content = file.read(5)<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>file.tell()<\/code> \u8fd4\u56de\u5f53\u524d\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e\uff0c<code>file.seek(0)<\/code> \u5c06\u6587\u4ef6\u6307\u9488\u79fb\u52a8\u5230\u6587\u4ef6\u5f00\u5934\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u76f8\u5bf9\u79fb\u52a8<\/h4>\n<\/p>\n<p><p><code>seek()<\/code> \u65b9\u6cd5\u8fd8\u53ef\u4ee5\u76f8\u5bf9\u5f53\u524d\u4f4d\u7f6e\u79fb\u52a8\u6587\u4ef6\u6307\u9488\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>    file.read(5)<\/p>\n<p>    file.seek(-2, 1)  # \u76f8\u5bf9\u5f53\u524d\u4f4d\u7f6e\u5411\u540e\u79fb\u52a82\u4e2a\u5b57\u8282<\/p>\n<p>    content = file.read(5)<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>file.seek(-2, 1)<\/code> \u5c06\u6587\u4ef6\u6307\u9488\u76f8\u5bf9\u5f53\u524d\u4f4d\u7f6e\u5411\u540e\u79fb\u52a82\u4e2a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6587\u4ef6\u7f16\u7801<\/h3>\n<\/p>\n<p><h4>1\u3001\u6307\u5b9a\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u975e\u5e38\u91cd\u8981\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u7684 <code>encoding<\/code> \u53c2\u6570\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>encoding=&#39;utf-8&#39;<\/code> \u6307\u5b9a\u4e86\u6587\u4ef6\u7f16\u7801\u4e3a UTF-8\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5199\u5165\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5199\u5165\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(&#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>encoding=&#39;utf-8&#39;<\/code> \u6307\u5b9a\u4e86\u5199\u5165\u6587\u4ef6\u65f6\u7684\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><h4>1\u3001\u6355\u83b7\u6587\u4ef6\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\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\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>try...except<\/code> \u8bed\u53e5\u6355\u83b7\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;nonexistent.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&#39;\u6587\u4ef6\u4e0d\u5b58\u5728&#39;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&#39;\u6743\u9650\u4e0d\u8db3&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>try...except<\/code> \u8bed\u53e5\u6355\u83b7\u4e86 <code>FileNotFoundError<\/code> \u548c <code>PermissionError<\/code> \u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u4e86\u76f8\u5e94\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u786e\u4fdd\u6587\u4ef6\u5173\u95ed<\/h4>\n<\/p>\n<p><p>\u5373\u4f7f\u5728\u51fa\u73b0\u5f02\u5e38\u65f6\uff0c\u6211\u4eec\u4e5f\u5e0c\u671b\u786e\u4fdd\u6587\u4ef6\u80fd\u591f\u6b63\u786e\u5173\u95ed\u3002\u6211\u4eec\u53ef\u4ee5\u5728 <code>finally<\/code> \u5757\u4e2d\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = None<\/p>\n<p>try:<\/p>\n<p>    file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&#39;\u6587\u4ef6\u4e0d\u5b58\u5728&#39;)<\/p>\n<p>finally:<\/p>\n<p>    if file:<\/p>\n<p>        file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>finally<\/code> \u5757\u786e\u4fdd\u4e86\u6587\u4ef6\u5728\u51fa\u73b0\u5f02\u5e38\u65f6\u80fd\u591f\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u6848\u4f8b\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1\u3001\u7edf\u8ba1\u6587\u4ef6\u5b57\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u7edf\u8ba1\u6587\u4ef6\u4e2d\u7684\u5b57\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_words(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        words = content.split()<\/p>\n<p>        return len(words)<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>word_count = count_words(file_path)<\/p>\n<p>print(f&#39;\u6587\u4ef6\u4e2d\u7684\u5b57\u6570\u4e3a: {word_count}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>count_words()<\/code> \u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u5c06\u5185\u5bb9\u6309\u7a7a\u683c\u5206\u5272\u4e3a\u5355\u8bcd\u5217\u8868\uff0c\u6700\u540e\u8fd4\u56de\u5355\u8bcd\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u590d\u5236\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5c06\u4e00\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def copy_file(src, dest):<\/p>\n<p>    with open(src, &#39;r&#39;, encoding=&#39;utf-8&#39;) as src_file:<\/p>\n<p>        with open(dest, &#39;w&#39;, encoding=&#39;utf-8&#39;) as dest_file:<\/p>\n<p>            content = src_file.read()<\/p>\n<p>            dest_file.write(content)<\/p>\n<p>src_file = &#39;example.txt&#39;<\/p>\n<p>dest_file = &#39;copy.txt&#39;<\/p>\n<p>copy_file(src_file, dest_file)<\/p>\n<p>print(f&#39;{src_file} \u7684\u5185\u5bb9\u5df2\u590d\u5236\u5230 {dest_file}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>copy_file()<\/code> \u51fd\u6570\u8bfb\u53d6\u6e90\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5199\u5165\u76ee\u6807\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 Python \u6253\u5f00\u548c\u6d4f\u89c8 txt \u6587\u6863\uff0c\u5305\u62ec\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u3001<code>with<\/code> \u8bed\u53e5\u3001\u9010\u884c\u8bfb\u53d6\u3001\u5199\u5165\u6587\u4ef6\u3001\u6587\u4ef6\u6a21\u5f0f\u3001\u6587\u4ef6\u6307\u9488\u64cd\u4f5c\u3001\u6587\u4ef6\u7f16\u7801\u3001\u5f02\u5e38\u5904\u7406\u7b49\u65b9\u9762\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5b9e\u9645\u6848\u4f8b\uff0c\u5982\u7edf\u8ba1\u6587\u4ef6\u5b57\u6570\u548c\u590d\u5236\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5b66\u4e60\u8fd9\u4e9b\u5185\u5bb9\uff0c\u60a8\u53ef\u4ee5\u66f4\u52a0\u719f\u7ec3\u5730\u4f7f\u7528 Python \u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\uff0c\u4ece\u800c\u66f4\u597d\u5730\u5904\u7406\u5404\u79cd\u6587\u672c\u6587\u4ef6\u4efb\u52a1\u3002\u8fd9\u4e9b\u6280\u80fd\u5728\u6570\u636e\u5904\u7406\u3001\u6587\u672c\u5206\u6790\u3001\u65e5\u5fd7\u5904\u7406\u7b49\u9886\u57df\u90fd\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u80fd\u591f\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u7075\u6d3b\u5e94\u7528\u8fd9\u4e9b\u77e5\u8bc6\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\u53d6txt\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u4f7f\u7528Python\u6253\u5f00\u5e76\u8bfb\u53d6txt\u6587\u4ef6\u7684\u8fc7\u7a0b\u76f8\u5bf9\u7b80\u5355\u3002\u53ef\u4ee5\u5229\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;yourfile.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>&#39;yourfile.txt&#39;<\/code>\u662f\u9700\u8981\u6253\u5f00\u7684\u6587\u4ef6\u540d\uff0c<code>&#39;r&#39;<\/code>\u8868\u793a\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6txt\u6587\u4ef6\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u9010\u884c\u8bfb\u53d6txt\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528<code>readline()<\/code>\u65b9\u6cd5\u6216\u76f4\u63a5\u8fed\u4ee3\u6587\u4ef6\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;yourfile.txt&#39;, &#39;r&#39;) as file:\n    for line in file:\n        print(line.strip())\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c<code>strip()<\/code>\u65b9\u6cd5\u7528\u4e8e\u53bb\u9664\u884c\u672b\u7684\u6362\u884c\u7b26\u548c\u7a7a\u683c\uff0c\u4f7f\u8f93\u51fa\u66f4\u52a0\u6574\u6d01\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6253\u5f00txt\u6587\u4ef6\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u6253\u5f00txt\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6743\u9650\u4e0d\u8db3\u3002\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u6765\u6355\u83b7\u8fd9\u4e9b\u9519\u8bef\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    with open(&#39;yourfile.txt&#39;, &#39;r&#39;) as file:\n        content = file.read()\n        print(content)\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u786e\u8ba4\u6587\u4ef6\u8def\u5f84\u662f\u5426\u6b63\u786e\u3002&quot;)\nexcept PermissionError:\n    print(&quot;\u6743\u9650\u4e0d\u8db3\uff0c\u65e0\u6cd5\u6253\u5f00\u8be5\u6587\u4ef6\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u4e86\u89e3\u5177\u4f53\u7684\u95ee\u9898\uff0c\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u6253\u5f00\u6d4f\u89c8txt\u6587\u6863\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6bd4\u5982\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 open()\u3001with\u8bed\u53e5\u3001\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u6216\u9010\u884c [&hellip;]","protected":false},"author":3,"featured_media":1176761,"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\/1176738"}],"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=1176738"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176738\/revisions"}],"predecessor-version":[{"id":1176764,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176738\/revisions\/1176764"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1176761"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1176738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1176738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1176738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}