{"id":1125514,"date":"2025-01-08T19:52:33","date_gmt":"2025-01-08T11:52:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1125514.html"},"modified":"2025-01-08T19:52:35","modified_gmt":"2025-01-08T11:52:35","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e4%b8%80%e4%b8%aa%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1125514.html","title":{"rendered":"python\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u6587\u672c\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090147\/8245bb4d-18bb-4302-955a-2b07c5a3d579.webp\" alt=\"python\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528open()\u51fd\u6570\u3001\u4f7f\u7528with\u8bed\u53e5\u3001\u5199\u5165\u6570\u636e\u5230\u6587\u4ef6\u3001\u4f7f\u7528\u4e0d\u540c\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528open()\u51fd\u6570<\/strong> \u662f\u6700\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u8ba9\u4f60\u4ee5\u4e0d\u540c\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528open()\u51fd\u6570\u662fPython\u4e2d\u6700\u57fa\u7840\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u901a\u8fc7open()\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u6216\u8005\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u7684\u6587\u4ef6\uff0c\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1aopen(filename, mode)\uff0c\u5176\u4e2dfilename\u662f\u6587\u4ef6\u540d\uff0cmode\u662f\u6587\u4ef6\u6253\u5f00\u7684\u6a21\u5f0f\uff0c\u5982&quot;w&quot;\u8868\u793a\u5199\u5165\u6a21\u5f0f\uff0c&quot;r&quot;\u8868\u793a\u8bfb\u53d6\u6a21\u5f0f\uff0c&quot;a&quot;\u8868\u793a\u8ffd\u52a0\u6a21\u5f0f\u7b49\u3002\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&quot;example.txt&quot;, &quot;w&quot;)<\/p>\n<p>file.write(&quot;Hello, World!&quot;)<\/p>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5c3d\u7ba1\u7b80\u5355\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u624b\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8bb2\u89e3Python\u751f\u6210\u6587\u672c\u6587\u4ef6\u7684\u5404\u79cd\u65b9\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528open()\u51fd\u6570\u751f\u6210\u6587\u672c\u6587\u4ef6<\/strong><\/h2>\n<p><h2>\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0copen()\u51fd\u6570\u662f\u6587\u4ef6\u64cd\u4f5c\u7684\u57fa\u7840\u3002open()\u51fd\u6570\u53ef\u4ee5\u4ee5\u4e0d\u540c\u7684\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5305\u62ec\u8bfb\uff08&quot;r&quot;\uff09\u3001\u5199\uff08&quot;w&quot;\uff09\u3001\u8ffd\u52a0\uff08&quot;a&quot;\uff09\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&quot;example.txt&quot;, &quot;w&quot;)<\/p>\n<p>file.write(&quot;Hello, World!&quot;)<\/p>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c&quot;example.txt&quot;\u662f\u6587\u4ef6\u540d\uff0c&quot;w&quot;\u8868\u793a\u5199\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0copen()\u51fd\u6570\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u8be5\u6587\u4ef6\u5c06\u88ab\u6e05\u7a7a\u3002<\/p>\n<\/p>\n<p><h2>\u4e0d\u540c\u6a21\u5f0f\u4ecb\u7ecd<\/h2>\n<\/p>\n<ul>\n<li><strong>&quot;r&quot; (Read mode)<\/strong>\uff1a\u4ee5\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1FileNotFoundError\u3002<\/li>\n<li><strong>&quot;w&quot; (Write mode)<\/strong>\uff1a\u4ee5\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\uff1b\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u4f1a\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u3002<\/li>\n<li><strong>&quot;a&quot; (Append mode)<\/strong>\uff1a\u4ee5\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\uff1b\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u65b0\u7684\u5185\u5bb9\u5c06\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\u3002<\/li>\n<li><strong>&quot;r+&quot; (Read and Write mode)<\/strong>\uff1a\u4ee5\u8bfb\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1FileNotFoundError\u3002<\/li>\n<\/ul>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u4e0d\u540c\u6a21\u5f0f\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5199\u5165\u6a21\u5f0f<\/p>\n<p>file = open(&quot;example.txt&quot;, &quot;w&quot;)<\/p>\n<p>file.write(&quot;This is a new file.&quot;)<\/p>\n<p>file.close()<\/p>\n<h2><strong>\u8bfb\u53d6\u6a21\u5f0f<\/strong><\/h2>\n<p>file = open(&quot;example.txt&quot;, &quot;r&quot;)<\/p>\n<p>content = file.read()<\/p>\n<p>print(content)<\/p>\n<p>file.close()<\/p>\n<h2><strong>\u8ffd\u52a0\u6a21\u5f0f<\/strong><\/h2>\n<p>file = open(&quot;example.txt&quot;, &quot;a&quot;)<\/p>\n<p>file.write(&quot;\\nThis is an appended line.&quot;)<\/p>\n<p>file.close()<\/p>\n<h2><strong>\u8bfb\u5199\u6a21\u5f0f<\/strong><\/h2>\n<p>file = open(&quot;example.txt&quot;, &quot;r+&quot;)<\/p>\n<p>file.write(&quot;Overwriting content.&quot;)<\/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\u751f\u6210\u6587\u672c\u6587\u4ef6<\/strong><\/h2>\n<p><h2>\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528with\u8bed\u53e5\u662f\u7ba1\u7406\u6587\u4ef6\u64cd\u4f5c\u7684\u63a8\u8350\u65b9\u6cd5\u3002with\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u8c03\u7528close()\u65b9\u6cd5\u3002\u8fd9\u4e0d\u4ec5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u8fd8\u80fd\u907f\u514d\u6587\u4ef6\u672a\u6b63\u786e\u5173\u95ed\u5bfc\u81f4\u7684\u8d44\u6e90\u6cc4\u6f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cwith\u8bed\u53e5\u4f1a\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\u3002\u5728\u9000\u51fawith\u5757\u65f6\uff0c\u6587\u4ef6\u4f1a\u88ab\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528\u591a\u4e2a\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528with\u8bed\u53e5\u540c\u65f6\u7ba1\u7406\u591a\u4e2a\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u591a\u4e2a\u6587\u4ef6\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example1.txt&quot;, &quot;w&quot;) as file1, open(&quot;example2.txt&quot;, &quot;w&quot;) as file2:<\/p>\n<p>    file1.write(&quot;Content for the first file.&quot;)<\/p>\n<p>    file2.write(&quot;Content for the second file.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e24\u4e2a\u6587\u4ef6\u90fd\u4f1a\u5728\u9000\u51fawith\u5757\u65f6\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u5199\u5165\u6570\u636e\u5230\u6587\u4ef6<\/strong><\/h2>\n<p><h2>\u5199\u5165\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u6700\u5e38\u89c1\u7684\u6587\u4ef6\u5199\u5165\u64cd\u4f5c\u662f\u5199\u5165\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5199\u5165\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5199\u5165\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5199\u5165\u5217\u8868\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&quot;First line&quot;, &quot;Second line&quot;, &quot;Third line&quot;]<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    for line in lines:<\/p>\n<p>        file.write(line + &quot;\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a\u5217\u8868\u5143\u7d20\u90fd\u4f1a\u88ab\u5199\u5165\u6587\u4ef6\uff0c\u5e76\u5728\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h2>\u5199\u5165\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178\u5e76\u4e14\u60f3\u5c06\u5176\u5185\u5bb9\u5199\u5165\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    for key, value in data.items():<\/p>\n<p>        file.write(f&quot;{key}: {value}\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u5178\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\u90fd\u4f1a\u88ab\u5199\u5165\u6587\u4ef6\uff0c\u5e76\u5728\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528\u4e0d\u540c\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p><h2>\u8bfb\u6a21\u5f0f<\/h2>\n<\/p>\n<p><p>\u8bfb\u6a21\u5f0f\uff08&quot;r&quot;\uff09\u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1FileNotFoundError\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5199\u6a21\u5f0f<\/h2>\n<\/p>\n<p><p>\u5199\u6a21\u5f0f\uff08&quot;w&quot;\uff09\u7528\u4e8e\u5199\u5165\u6587\u4ef6\u5185\u5bb9\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\uff1b\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u4f1a\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;This is a new file.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u8ffd\u52a0\u6a21\u5f0f<\/h2>\n<\/p>\n<p><p>\u8ffd\u52a0\u6a21\u5f0f\uff08&quot;a&quot;\uff09\u7528\u4e8e\u5c06\u65b0\u5185\u5bb9\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\uff1b\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u65b0\u7684\u5185\u5bb9\u5c06\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;a&quot;) as file:<\/p>\n<p>    file.write(&quot;\\nThis is an appended line.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u8bfb\u5199\u6a21\u5f0f<\/h2>\n<\/p>\n<p><p>\u8bfb\u5199\u6a21\u5f0f\uff08&quot;r+&quot;\uff09\u7528\u4e8e\u540c\u65f6\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u5185\u5bb9\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1FileNotFoundError\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;r+&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    file.write(&quot;\\nAdding new content.&quot;)<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6587\u4ef6\u5185\u5bb9\u4f1a\u88ab\u8bfb\u53d6\u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\uff0c\u7136\u540e\u65b0\u7684\u5185\u5bb9\u4f1a\u88ab\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u5904\u7406\u5927\u6587\u4ef6<\/strong><\/h2>\n<p><h2>\u5206\u5757\u8bfb\u53d6<\/h2>\n<\/p>\n<p><p>\u5f53\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u4e0d\u8db3\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u5206\u5757\u8bfb\u53d6\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5206\u5757\u8bfb\u53d6\u6587\u4ef6\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">chunk_size = 1024  # 1KB<\/p>\n<p>with open(&quot;large_file.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    while True:<\/p>\n<p>        chunk = file.read(chunk_size)<\/p>\n<p>        if not chunk:<\/p>\n<p>            break<\/p>\n<p>        print(chunk)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6587\u4ef6\u4f1a\u88ab\u5206\u5757\u8bfb\u53d6\uff0c\u6bcf\u6b21\u8bfb\u53d61KB\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u5206\u884c\u8bfb\u53d6<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6587\u4ef6\u6309\u884c\u7ec4\u7ec7\uff0c\u53ef\u4ee5\u4f7f\u7528\u9010\u884c\u8bfb\u53d6\u6765\u5904\u7406\u5927\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;large_file.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line, end=&quot;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6587\u4ef6\u4f1a\u88ab\u9010\u884c\u8bfb\u53d6\uff0c\u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u5904\u7406\u6587\u4ef6\u5f02\u5e38<\/strong><\/h2>\n<p><h2>\u6355\u83b7\u5f02\u5e38<\/h2>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u8bfb\u5199\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u4e3a\u4e86\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6355\u83b7\u5f02\u5e38\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&quot;non_existent_file.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;File not found. Please check the file path.&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&quot;Permission denied. Please check your access rights.&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u4f1a\u88ab\u5206\u522b\u6355\u83b7\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/h2>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\u6765\u5904\u7406\u7279\u5b9a\u7684\u9519\u8bef\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class CustomFileError(Exception):<\/p>\n<p>    pass<\/p>\n<p>try:<\/p>\n<p>    with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        if &quot;error&quot; in content:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se CustomFileError(&quot;Custom error found in file content.&quot;)<\/p>\n<p>except CustomFileError as e:<\/p>\n<p>    print(e)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u6587\u4ef6\u5185\u5bb9\u4e2d\u5305\u542b\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\uff0c\u4f1a\u5f15\u53d1\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u6587\u4ef6\u8def\u5f84\u4e0e\u6587\u4ef6\u540d<\/strong><\/h2>\n<p><h2>\u76f8\u5bf9\u8def\u5f84\u4e0e\u7edd\u5bf9\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u8def\u5f84\u53ef\u4ee5\u662f\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\u3002\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\uff0c\u800c\u7edd\u5bf9\u8def\u5f84\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u76f8\u5bf9\u8def\u5f84<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;This is a file with a relative path.&quot;)<\/p>\n<h2><strong>\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>with open(&quot;\/home\/user\/example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;This is a file with an absolute path.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u83b7\u53d6\u6587\u4ef6\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_dir = os.getcwd()<\/p>\n<p>print(f&quot;Current directory: {current_dir}&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.abspath(&quot;example.txt&quot;)<\/p>\n<p>print(f&quot;File path: {file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cos.getcwd()\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0cos.path.abspath()\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u6587\u4ef6\u7f16\u7801<\/strong><\/h2>\n<p><h2>\u6307\u5b9a\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u975eASCII\u5b57\u7b26\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u6587\u4ef6\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u8bfb\u53d6\u4e0d\u540c\u7f16\u7801\u7684\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u8bfb\u53d6\u4e0d\u540c\u7f16\u7801\u7684\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8bfb\u53d6\u4e0d\u540c\u7f16\u7801\u6587\u4ef6\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) 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\u6587\u4ef6\u4f1a\u88ab\u4ee5UTF-8\u7f16\u7801\u6253\u5f00\u5e76\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u6587\u4ef6\u64cd\u4f5c\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/h2>\n<p><h2>\u4f7f\u7528with\u8bed\u53e5<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528with\u8bed\u53e5\u662f\u6587\u4ef6\u64cd\u4f5c\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u540e\u81ea\u52a8\u5173\u95ed\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528with\u8bed\u53e5\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u6355\u83b7\u5f02\u5e38<\/h2>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u6355\u83b7\u5f02\u5e38\u662f\u786e\u4fdd\u7a0b\u5e8f\u7a33\u5b9a\u8fd0\u884c\u7684\u53e6\u4e00\u6700\u4f73\u5b9e\u8df5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6355\u83b7\u5f02\u5e38\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&quot;non_existent_file.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;File not found. Please check the file path.&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&quot;Permission denied. Please check your access rights.&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u6307\u5b9a\u6587\u4ef6\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u975eASCII\u5b57\u7b26\u65f6\uff0c\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u6587\u4ef6\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u751f\u6210\u548c\u64cd\u4f5c\u6587\u672c\u6587\u4ef6\u3002\u65e0\u8bba\u662f\u57fa\u672c\u7684\u6587\u4ef6\u8bfb\u5199\uff0c\u8fd8\u662f\u5904\u7406\u5927\u6587\u4ef6\u548c\u5f02\u5e38\u6355\u83b7\uff0c\u8fd9\u4e9b\u77e5\u8bc6\u90fd\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5b8c\u6210\u4efb\u52a1\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\u7684\u6587\u672c\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\u7684\u6587\u672c\u6587\u4ef6\u3002\u53ea\u9700\u5c06\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\uff08\u5982\u201cw\u201d\u8868\u793a\u5199\u5165\u6a21\u5f0f\uff09\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>open(&#39;myfile.txt&#39;, &#39;w&#39;)<\/code>\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>myfile.txt<\/code>\u7684\u65b0\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5b83\u5c06\u88ab\u8986\u76d6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5411\u6587\u672c\u6587\u4ef6\u4e2d\u5199\u5165\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u5411\u6587\u672c\u6587\u4ef6\u4e2d\u5199\u5165\u5185\u5bb9\uff0c\u53ef\u4ee5\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u4f7f\u7528\u201cw\u201d\u6a21\u5f0f\u6216\u201ca\u201d\u6a21\u5f0f\uff08\u8ffd\u52a0\u6a21\u5f0f\uff09\u3002\u4f7f\u7528\u201cw\u201d\u6a21\u5f0f\u65f6\uff0c\u4f7f\u7528<code>file.write(&#39;\u5185\u5bb9&#39;)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;myfile.txt&#39;, &#39;w&#39;) as file:\n    file.write(&#39;Hello, World!&#39;)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u521b\u5efa\u6587\u4ef6\u5e76\u5199\u5165\u201cHello, World!\u201d\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bfb\u53d6\u5df2\u521b\u5efa\u7684\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u3002\u4f7f\u7528\u201cr\u201d\u6a21\u5f0f\uff08\u8bfb\u53d6\u6a21\u5f0f\uff09\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>file.read()<\/code>\u6216<code>file.readlines()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u5185\u5bb9\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;myfile.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5c06\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u5185\u5bb9\u8bfb\u53d6\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528open()\u51fd\u6570\u3001\u4f7f\u7528with\u8bed\u53e5\u3001\u5199\u5165\u6570\u636e\u5230\u6587\u4ef6\u3001\u4f7f\u7528\u4e0d\u540c\u6a21\u5f0f\u6253 [&hellip;]","protected":false},"author":3,"featured_media":1125519,"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\/1125514"}],"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=1125514"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125514\/revisions"}],"predecessor-version":[{"id":1125520,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125514\/revisions\/1125520"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1125519"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1125514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1125514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1125514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}