{"id":1156420,"date":"2025-01-13T18:15:56","date_gmt":"2025-01-13T10:15:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156420.html"},"modified":"2025-01-13T18:15:59","modified_gmt":"2025-01-13T10:15:59","slug":"python%e7%bc%96%e7%a8%8b%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e4%bf%9d%e5%ad%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1156420.html","title":{"rendered":"Python\u7f16\u7a0b\u5982\u4f55\u6253\u5f00\u4fdd\u5b58"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25194903\/e159e9d8-f84d-47c2-9939-4c75f2f3ab68.webp\" alt=\"Python\u7f16\u7a0b\u5982\u4f55\u6253\u5f00\u4fdd\u5b58\" \/><\/p>\n<p><p> <strong>Python\u7f16\u7a0b\u5982\u4f55\u6253\u5f00\u4fdd\u5b58<\/strong>\uff1a\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u3001\u4f7f\u7528with\u8bed\u53e5\u3001\u6307\u5b9a\u6587\u4ef6\u6a21\u5f0f\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002<strong>\u5176\u4e2d\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u662f\u6700\u5173\u952e\u7684\u6b65\u9aa4<\/strong>\u3002<code>open()<\/code>\u51fd\u6570\u662fPython\u4e2d\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u7684\u57fa\u7840\u51fd\u6570\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u5b9e\u73b0\u6587\u4ef6\u7684\u6253\u5f00\u548c\u4fdd\u5b58\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>open(file, mode)<\/code>\uff0c\u5176\u4e2d<code>file<\/code>\u662f\u6587\u4ef6\u540d\uff0c<code>mode<\/code>\u662f\u6253\u5f00\u6587\u4ef6\u7684\u6a21\u5f0f\uff0c\u6bd4\u5982&#39;r&#39;\u8868\u793a\u53ea\u8bfb\uff0c&#39;w&#39;\u8868\u793a\u5199\u5165\uff0c&#39;a&#39;\u8868\u793a\u8ffd\u52a0\u7b49\u7b49\u3002\u63a5\u4e0b\u6765\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c<code>open()<\/code>\u51fd\u6570\u662f\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\u7684\u57fa\u7840\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">open(file, mode=&#39;r&#39;, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>1.1 \u57fa\u672c\u53c2\u6570<\/h2>\n<\/p>\n<ul>\n<li><code>file<\/code>\uff1a\u6587\u4ef6\u540d\u6216\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<li><code>mode<\/code>\uff1a\u6587\u4ef6\u6253\u5f00\u6a21\u5f0f\uff0c\u5982&#39;r&#39;, &#39;w&#39;, &#39;a&#39;, &#39;b&#39;, &#39;t&#39;, &#39;+&#39;\u7b49\u3002<\/li>\n<li><code>buffering<\/code>\uff1a\u8bbe\u7f6e\u6587\u4ef6\u7684\u7f13\u51b2\u7b56\u7565\u3002<\/li>\n<li><code>encoding<\/code>\uff1a\u6587\u4ef6\u7f16\u7801\u7c7b\u578b\u3002<\/li>\n<li><code>errors<\/code>\uff1a\u9519\u8bef\u5904\u7406\u7b56\u7565\u3002<\/li>\n<li><code>newline<\/code>\uff1a\u63a7\u5236\u884c\u7ed3\u675f\u7b26\u7684\u5904\u7406\u3002<\/li>\n<li><code>closefd<\/code>\uff1a\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li><code>opener<\/code>\uff1a\u81ea\u5b9a\u4e49\u6253\u5f00\u5668\u3002<\/li>\n<\/ul>\n<p><h2>1.2 \u5e38\u7528\u6a21\u5f0f<\/h2>\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\uff08\u4f1a\u8986\u76d6\u6587\u4ef6\uff09\u3002<\/li>\n<li><code>&#39;a&#39;<\/code>\uff1a\u8ffd\u52a0\u6a21\u5f0f\uff08\u6587\u4ef6\u6307\u9488\u5728\u672b\u5c3e\uff09\u3002<\/li>\n<li><code>&#39;b&#39;<\/code>\uff1a\u4e8c\u8fdb\u5236\u6a21\u5f0f\u3002<\/li>\n<li><code>&#39;t&#39;<\/code>\uff1a\u6587\u672c\u6a21\u5f0f\uff08\u9ed8\u8ba4\uff09\u3002<\/li>\n<li><code>&#39;+&#39;<\/code>\uff1a\u66f4\u65b0\u6a21\u5f0f\uff08\u8bfb\u5199\uff09\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\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<h2><strong>\u4e8c\u3001\u4f7f\u7528with\u8bed\u53e5<\/strong><\/h2>\n<p><p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u66f4\u7b80\u6d01\u548c\u5b89\u5168\u5730\u5904\u7406\u6587\u4ef6\u3002<code>with<\/code>\u8bed\u53e5\u4f1a\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u7684\u5173\u95ed\u64cd\u4f5c\uff0c\u5373\u4f7f\u5728\u51fa\u73b0\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\u4e5f\u80fd\u4fdd\u8bc1\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h2>2.1 \u57fa\u672c\u7528\u6cd5<\/h2>\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\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>with open(&#39;example.txt&#39;, &#39;r&#39;) as file<\/code>\u4f1a\u6253\u5f00\u6587\u4ef6\u5e76\u5c06\u6587\u4ef6\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cf<code>file<\/code>\uff0c<code>with<\/code>\u5757\u7ed3\u675f\u540e\uff0c\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h2>2.2 \u5904\u7406\u591a\u4e2a\u6587\u4ef6<\/h2>\n<\/p>\n<p><p><code>with<\/code>\u8bed\u53e5\u8fd8\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example1.txt&#39;, &#39;r&#39;) as file1, open(&#39;example2.txt&#39;, &#39;w&#39;) as file2:<\/p>\n<p>    content = file1.read()<\/p>\n<p>    file2.write(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>file1<\/code>\u548c<code>file2<\/code>\u5206\u522b\u8868\u793a\u4e24\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u5bf9\u8c61\uff0c<code>file1<\/code>\u7528\u4e8e\u8bfb\u53d6\uff0c<code>file2<\/code>\u7528\u4e8e\u5199\u5165\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u6307\u5b9a\u6587\u4ef6\u6a21\u5f0f<\/strong><\/h2>\n<p><p>\u6587\u4ef6\u6a21\u5f0f\u51b3\u5b9a\u4e86\u6587\u4ef6\u7684\u6253\u5f00\u65b9\u5f0f\uff0c\u4f8b\u5982\u53ea\u8bfb\u3001\u5199\u5165\u3001\u8ffd\u52a0\u7b49\u3002\u4e0d\u540c\u6a21\u5f0f\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>3.1 \u53ea\u8bfb\u6a21\u5f0f<\/h2>\n<\/p>\n<p><p>\u53ea\u8bfb\u6a21\u5f0f\u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u4e0d\u4f1a\u4fee\u6539\u6587\u4ef6\u3002\u5e38\u7528\u7684\u6a21\u5f0f\u6709<code>&#39;r&#39;<\/code>\u548c<code>&#39;rb&#39;<\/code>\uff08\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff09\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><h2>3.2 \u5199\u5165\u6a21\u5f0f<\/h2>\n<\/p>\n<p><p>\u5199\u5165\u6a21\u5f0f\u7528\u4e8e\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0c\u4f1a\u8986\u76d6\u5df2\u6709\u5185\u5bb9\u3002\u5e38\u7528\u7684\u6a21\u5f0f\u6709<code>&#39;w&#39;<\/code>\u548c<code>&#39;wb&#39;<\/code>\uff08\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff09\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><h2>3.3 \u8ffd\u52a0\u6a21\u5f0f<\/h2>\n<\/p>\n<p><p>\u8ffd\u52a0\u6a21\u5f0f\u7528\u4e8e\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u6570\u636e\uff0c\u4e0d\u4f1a\u8986\u76d6\u5df2\u6709\u5185\u5bb9\u3002\u5e38\u7528\u7684\u6a21\u5f0f\u6709<code>&#39;a&#39;<\/code>\u548c<code>&#39;ab&#39;<\/code>\uff08\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff09\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;Appended text.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u5904\u7406\u6587\u4ef6\u8def\u5f84\u662f\u4e00\u4e2a\u91cd\u8981\u73af\u8282\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h2>4.1 \u7edd\u5bf9\u8def\u5f84\u548c\u76f8\u5bf9\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u7edd\u5bf9\u8def\u5f84\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u800c\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7edd\u5bf9\u8def\u5f84<\/p>\n<p>with open(&#39;\/home\/user\/example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<h2><strong>\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\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<p><\/code><\/pre>\n<\/p>\n<p><h2>4.2 \u4f7f\u7528os\u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u6587\u4ef6\u8def\u5f84\u7684\u51fd\u6570\uff0c\u5982<code>os.path.join()<\/code>\u3001<code>os.path.dirname()<\/code>\u3001<code>os.path.basename()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(&#39;folder&#39;, &#39;example.txt&#39;)<\/p>\n<p>with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u76ee\u5f55\u548c\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>directory = os.path.dirname(file_path)<\/p>\n<p>filename = os.path.basename(file_path)<\/p>\n<p>print(f&#39;Directory: {directory}, Filename: {filename}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4.3 \u4f7f\u7528pathlib\u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u8def\u5f84\u5904\u7406\u65b9\u6cd5\uff0c\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efaPath\u5bf9\u8c61<\/strong><\/h2>\n<p>file_path = Path(&#39;folder&#39;) \/ &#39;example.txt&#39;<\/p>\n<p>with file_path.open(&#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u76ee\u5f55\u548c\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>directory = file_path.parent<\/p>\n<p>filename = file_path.name<\/p>\n<p>print(f&#39;Directory: {directory}, Filename: {filename}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u5904\u7406\u6587\u4ef6\u5f02\u5e38<\/strong><\/h2>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u5904\u7406\u5f02\u5e38\u662f\u4e00\u4e2a\u91cd\u8981\u73af\u8282\uff0c\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u80fd\u591f\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>5.1 \u4f7f\u7528try-except\u8bed\u53e5<\/h2>\n<\/p>\n<p><p><code>try-except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\u4e2d\u7684\u5f02\u5e38\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/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<p>except FileNotFoundError:<\/p>\n<p>    print(&#39;File not found.&#39;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&#39;Permission denied.&#39;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&#39;An error occurred: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>5.2 \u4f7f\u7528finally\u8bed\u53e5<\/h2>\n<\/p>\n<p><p><code>finally<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u65e0\u8bba\u662f\u5426\u51fa\u73b0\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\uff0c\u5982\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 Exception as e:<\/p>\n<p>    print(f&#39;An error occurred: {e}&#39;)<\/p>\n<p>finally:<\/p>\n<p>    if file:<\/p>\n<p>        file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u8bfb\u5199\u5927\u6587\u4ef6<\/strong><\/h2>\n<p><p>\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u5185\u5b58\u548c\u6027\u80fd\u95ee\u9898\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u9ad8\u6548\u5730\u5904\u7406\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>6.1 \u5206\u5757\u8bfb\u53d6<\/h2>\n<\/p>\n<p><p>\u5206\u5757\u8bfb\u53d6\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u5c06\u5927\u6587\u4ef6\u8bfb\u5165\u5185\u5b58\uff0c\u4ece\u800c\u51cf\u5c11\u5185\u5b58\u5360\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)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>6.2 \u4f7f\u7528\u8fed\u4ee3\u5668<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684\u8fed\u4ee3\u5668\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6587\u4ef6\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)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>6.3 \u5185\u5b58\u6620\u5c04<\/h2>\n<\/p>\n<p><p>\u5185\u5b58\u6620\u5c04\uff08Memory Mapping\uff09\u53ef\u4ee5\u5c06\u6587\u4ef6\u6620\u5c04\u5230\u5185\u5b58\uff0c\u9002\u7528\u4e8e\u8bfb\u5199\u5927\u6587\u4ef6\u3002Python\u7684<code>mmap<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5185\u5b58\u6620\u5c04\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mmap<\/p>\n<p>with open(&#39;large_file.txt&#39;, &#39;r+b&#39;) as file:<\/p>\n<p>    with mmap.mmap(file.fileno(), 0) as mm:<\/p>\n<p>        print(mm[:100])  # \u8bfb\u53d6\u524d100\u4e2a\u5b57\u8282<\/p>\n<p>        mm[0] = b&#39;X&#39;  # \u4fee\u6539\u7b2c\u4e00\u4e2a\u5b57\u8282<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u6587\u672c\u6587\u4ef6\u548c\u4e8c\u8fdb\u5236\u6587\u4ef6<\/strong><\/h2>\n<p><p>\u5904\u7406\u6587\u672c\u6587\u4ef6\u548c\u4e8c\u8fdb\u5236\u6587\u4ef6\u6709\u4e0d\u540c\u7684\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h2>7.1 \u6587\u672c\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u6587\u672c\u6587\u4ef6\u901a\u5e38\u5305\u542b\u53ef\u8bfb\u7684\u5b57\u7b26\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u6a21\u5f0f\u6253\u5f00\uff0c\u5982<code>&#39;r&#39;<\/code>\u3001<code>&#39;w&#39;<\/code>\u3001<code>&#39;a&#39;<\/code>\u7b49\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><h2>7.2 \u4e8c\u8fdb\u5236\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u4e8c\u8fdb\u5236\u6587\u4ef6\u5305\u542b\u975e\u6587\u672c\u6570\u636e\uff0c\u5982\u56fe\u50cf\u3001\u97f3\u9891\u7b49\uff0c\u9700\u8981\u4f7f\u7528\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\uff0c\u5982<code>&#39;rb&#39;<\/code>\u3001<code>&#39;wb&#39;<\/code>\u3001<code>&#39;ab&#39;<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.jpg&#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<h2><strong>\u516b\u3001\u6587\u4ef6\u7f16\u7801<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u6587\u4ef6\u7f16\u7801\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u5e38\u89c1\u7684\u7f16\u7801\u6709UTF-8\u3001ASCII\u3001ISO-8859-1\u7b49\u3002<\/p>\n<\/p>\n<p><h2>8.1 \u6307\u5b9a\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u6253\u5f00\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<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><h2>8.2 \u5904\u7406\u7f16\u7801\u9519\u8bef<\/h2>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f16\u7801\u9519\u8bef\uff0c\u53ef\u4ee5\u901a\u8fc7<code>errors<\/code>\u53c2\u6570\u6307\u5b9a\u9519\u8bef\u5904\u7406\u7b56\u7565\uff0c\u5982<code>&#39;ignore&#39;<\/code>\u3001<code>&#39;replace&#39;<\/code>\u7b49\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;, errors=&#39;ignore&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e5d\u3001\u6587\u4ef6\u5bf9\u8c61\u65b9\u6cd5<\/strong><\/h2>\n<p><p>\u6587\u4ef6\u5bf9\u8c61\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u64cd\u4f5c\u6587\u4ef6\uff0c\u5982\u8bfb\u5199\u3001\u5b9a\u4f4d\u3001\u5237\u65b0\u7b49\u3002<\/p>\n<\/p>\n<p><h2>9.1 \u8bfb\u53d6\u65b9\u6cd5<\/h2>\n<\/p>\n<ul>\n<li><code>read(size)<\/code>\uff1a\u8bfb\u53d6\u6307\u5b9a\u5927\u5c0f\u7684\u6570\u636e\u3002<\/li>\n<li><code>readline()<\/code>\uff1a\u8bfb\u53d6\u4e00\u884c\u6570\u636e\u3002<\/li>\n<li><code>readlines()<\/code>\uff1a\u8bfb\u53d6\u6240\u6709\u884c\u6570\u636e\uff0c\u8fd4\u56de\u5217\u8868\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read(100)<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>9.2 \u5199\u5165\u65b9\u6cd5<\/h2>\n<\/p>\n<ul>\n<li><code>write(data)<\/code>\uff1a\u5199\u5165\u6570\u636e\u3002<\/li>\n<li><code>writelines(lines)<\/code>\uff1a\u5199\u5165\u591a\u884c\u6570\u636e\uff0c\u53c2\u6570\u4e3a\u5217\u8868\u3002<\/li>\n<\/ul>\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>    file.writelines([&#39;Line 1\\n&#39;, &#39;Line 2\\n&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>9.3 \u5b9a\u4f4d\u65b9\u6cd5<\/h2>\n<\/p>\n<ul>\n<li><code>seek(offset, whence)<\/code>\uff1a\u79fb\u52a8\u6587\u4ef6\u6307\u9488\u3002<\/li>\n<li><code>tell()<\/code>\uff1a\u8fd4\u56de\u6587\u4ef6\u6307\u9488\u5f53\u524d\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    file.seek(10, 0)<\/p>\n<p>    print(file.tell())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>9.4 \u5237\u65b0\u65b9\u6cd5<\/h2>\n<\/p>\n<ul>\n<li><code>flush()<\/code>\uff1a\u5237\u65b0\u6587\u4ef6\u7f13\u51b2\u533a\uff0c\u5c06\u7f13\u51b2\u533a\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002<\/li>\n<\/ul>\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>    file.flush()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u3001\u6587\u4ef6\u4e0e\u76ee\u5f55\u64cd\u4f5c<\/strong><\/h2>\n<p><p>\u9664\u4e86\u6587\u4ef6\u64cd\u4f5c\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u76ee\u5f55\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u521b\u5efa\u3001\u5220\u9664\u3001\u904d\u5386\u7b49\u3002<\/p>\n<\/p>\n<p><h2>10.1 \u521b\u5efa\u548c\u5220\u9664\u76ee\u5f55<\/h2>\n<\/p>\n<ul>\n<li><code>os.mkdir(path)<\/code>\uff1a\u521b\u5efa\u76ee\u5f55\u3002<\/li>\n<li><code>os.makedirs(path)<\/code>\uff1a\u9012\u5f52\u521b\u5efa\u76ee\u5f55\u3002<\/li>\n<li><code>os.rmdir(path)<\/code>\uff1a\u5220\u9664\u76ee\u5f55\u3002<\/li>\n<li><code>os.removedirs(path)<\/code>\uff1a\u9012\u5f52\u5220\u9664\u76ee\u5f55\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>os.mkdir(&#39;new_folder&#39;)<\/p>\n<p>os.makedirs(&#39;nested\/folder&#39;)<\/p>\n<p>os.rmdir(&#39;new_folder&#39;)<\/p>\n<p>os.removedirs(&#39;nested\/folder&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>10.2 \u904d\u5386\u76ee\u5f55<\/h2>\n<\/p>\n<ul>\n<li><code>os.listdir(path)<\/code>\uff1a\u8fd4\u56de\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u5217\u8868\u3002<\/li>\n<li><code>os.walk(path)<\/code>\uff1a\u751f\u6210\u76ee\u5f55\u6811\u7684\u6587\u4ef6\u540d\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">for root, dirs, files in os.walk(&#39;.&#39;):<\/p>\n<p>    print(f&#39;Root: {root}&#39;)<\/p>\n<p>    print(f&#39;Dirs: {dirs}&#39;)<\/p>\n<p>    print(f&#39;Files: {files}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>10.3 \u6587\u4ef6\u548c\u76ee\u5f55\u7684\u5176\u4ed6\u64cd\u4f5c<\/h2>\n<\/p>\n<ul>\n<li><code>os.rename(src, dst)<\/code>\uff1a\u91cd\u547d\u540d\u6587\u4ef6\u6216\u76ee\u5f55\u3002<\/li>\n<li><code>os.remove(path)<\/code>\uff1a\u5220\u9664\u6587\u4ef6\u3002<\/li>\n<li><code>shutil.copy(src, dst)<\/code>\uff1a\u590d\u5236\u6587\u4ef6\u3002<\/li>\n<li><code>shutil.move(src, dst)<\/code>\uff1a\u79fb\u52a8\u6587\u4ef6\u6216\u76ee\u5f55\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>os.rename(&#39;example.txt&#39;, &#39;renamed_example.txt&#39;)<\/p>\n<p>os.remove(&#39;renamed_example.txt&#39;)<\/p>\n<p>shutil.copy(&#39;source.txt&#39;, &#39;destination.txt&#39;)<\/p>\n<p>shutil.move(&#39;source_folder&#39;, &#39;destination_folder&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u4e2a\u65b9\u9762\u7684\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u5168\u9762\u4e86\u89e3Python\u4e2d\u5982\u4f55\u6253\u5f00\u548c\u4fdd\u5b58\u6587\u4ef6\uff0c\u4ee5\u53ca\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3001\u5f02\u5e38\u3001\u7f16\u7801\u3001\u8bfb\u5199\u65b9\u6cd5\u548c\u76ee\u5f55\u64cd\u4f5c\u7b49\u591a\u4e2a\u65b9\u9762\u7684\u77e5\u8bc6\u3002\u638c\u63e1\u8fd9\u4e9b\u77e5\u8bc6\u53ef\u4ee5\u5e2e\u52a9\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u5141\u8bb8\u4f60\u6307\u5b9a\u6587\u4ef6\u7684\u8def\u5f84\u548c\u6a21\u5f0f\uff08\u4f8b\u5982\uff0c\u53ea\u8bfb\u3001\u5199\u5165\u7b49\uff09\u3002\u4f8b\u5982\uff0c<code>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/code>\u5c06\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u540d\u4e3a<code>example.txt<\/code>\u7684\u6587\u4ef6\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u9632\u6b62\u6587\u4ef6\u6cc4\u9732\u6216\u5360\u7528\u8d44\u6e90\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4fdd\u5b58\u6570\u636e\u5230\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u7ed3\u5408\u5199\u5165\u6a21\u5f0f\uff08\u5982<code>&#39;w&#39;<\/code>\u6216<code>&#39;a&#39;<\/code>\uff09\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u6216\u8986\u76d6\u73b0\u6709\u6587\u4ef6\u3002\u4f7f\u7528<code>file.write(data)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u6b64\u5916\uff0c<code>file.writelines(list_of_lines)<\/code>\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u591a\u884c\u6587\u672c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6587\u4ef6\u6253\u5f00\u548c\u4fdd\u5b58\u4e2d\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f8b\u5982\uff0c<code>try:<\/code>\u5757\u4e2d\u653e\u7f6e\u6253\u5f00\u6587\u4ef6\u7684\u4ee3\u7801\uff0c\u800c\u5728<code>except FileNotFoundError:<\/code>\u5757\u4e2d\u5904\u7406\u6587\u4ef6\u672a\u627e\u5230\u7684\u60c5\u51b5\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u5f02\u5e38\u800c\u5d29\u6e83\uff0c\u5e76\u63d0\u4f9b\u7528\u6237\u53cb\u597d\u7684\u9519\u8bef\u63d0\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7f16\u7a0b\u5982\u4f55\u6253\u5f00\u4fdd\u5b58\uff1a\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u3001\u4f7f\u7528with\u8bed\u53e5\u3001\u6307\u5b9a\u6587\u4ef6\u6a21\u5f0f\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002\u5176\u4e2d\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1156430,"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\/1156420"}],"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=1156420"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156420\/revisions"}],"predecessor-version":[{"id":1156434,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156420\/revisions\/1156434"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1156430"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1156420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1156420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1156420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}