{"id":1091772,"date":"2025-01-08T14:14:58","date_gmt":"2025-01-08T06:14:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1091772.html"},"modified":"2025-01-08T14:15:01","modified_gmt":"2025-01-08T06:15:01","slug":"python-%e5%a6%82%e4%bd%95%e5%86%99%e6%96%87%e4%bb%b6%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1091772.html","title":{"rendered":"python \u5982\u4f55\u5199\u6587\u4ef6\u64cd\u4f5c\u7cfb\u7edf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205127\/8f5aefdb-c3ac-4b59-b157-666136078a99.webp\" alt=\"python \u5982\u4f55\u5199\u6587\u4ef6\u64cd\u4f5c\u7cfb\u7edf\" \/><\/p>\n<p><p> \u4e00\u3001Python\u5199\u6587\u4ef6\u64cd\u4f5c\u7cfb\u7edf\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528open()\u51fd\u6570\u3001\u5199\u5165\u64cd\u4f5c\u3001\u5173\u95ed\u6587\u4ef6<\/strong>\uff0c\u5728Python\u4e2d\uff0c\u5199\u6587\u4ef6\u64cd\u4f5c\u7cfb\u7edf\u7684\u57fa\u672c\u6b65\u9aa4\u4e3b\u8981\u5305\u62ec\u6253\u5f00\u6587\u4ef6\u3001\u5199\u5165\u6570\u636e\u4ee5\u53ca\u5173\u95ed\u6587\u4ef6\u3002\u4f7f\u7528open()\u51fd\u6570\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\uff0c\u5e38\u7528\u7684\u6a21\u5f0f\u6709\u5199\u6a21\u5f0f(&#39;w&#39;)\u3001\u8ffd\u52a0\u6a21\u5f0f(&#39;a&#39;)\u7b49\u3002\u5199\u5165\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7write()\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5b8c\u6210\u5199\u5165\u64cd\u4f5c\u540e\uff0c\u5fc5\u987b\u4f7f\u7528close()\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u4fdd\u5b58\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528open()\u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\u3002\u8be5\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(filename, mode)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0cfilename\u662f\u8981\u6253\u5f00\u7684\u6587\u4ef6\u540d\uff0cmode\u662f\u6587\u4ef6\u6253\u5f00\u7684\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\uff09<\/li>\n<li>&#39;w&#39;\uff1a\u5199\u5165\u6a21\u5f0f\uff08\u4f1a\u8986\u76d6\u6587\u4ef6\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\uff08\u53ef\u4e0e\u5176\u4ed6\u6a21\u5f0f\u7ed3\u5408\u4f7f\u7528\uff09<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a\u201cexample.txt\u201d\u7684\u6587\u4ef6\uff0c\u4f7f\u7528\u5199\u5165\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&quot;example.txt&quot;, &quot;w&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5199\u5165\u6570\u636e<\/li>\n<\/ol>\n<p><p>\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528write()\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.write(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cwrite()\u65b9\u6cd5\u4e0d\u4f1a\u81ea\u52a8\u5728\u6570\u636e\u672b\u5c3e\u6dfb\u52a0\u6362\u884c\u7b26\u3002\u5982\u679c\u9700\u8981\u6362\u884c\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u624b\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\uff08\\n\uff09\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5173\u95ed\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u5b8c\u6210\u5199\u5165\u64cd\u4f5c\u540e\uff0c\u5fc5\u987b\u4f7f\u7528close()\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u4fdd\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528with\u8bed\u53e5\u7b80\u5316\u6587\u4ef6\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528with\u8bed\u53e5\u7b80\u5316\u6587\u4ef6\u64cd\u4f5c\uff0c\u907f\u514d\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\u3002with\u8bed\u53e5\u4f1a\u5728\u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6bd5\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3002\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(filename, mode) as file:<\/p>\n<p>    file.write(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\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\uff0c\u6587\u4ef6\u5728with\u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6bd5\u540e\u4f1a\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5199\u5165\u591a\u884c\u6570\u636e<\/p>\n<\/p>\n<p><p>\u4f7f\u7528write()\u65b9\u6cd5\u5199\u5165\u591a\u884c\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6216writelines()\u65b9\u6cd5\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5faa\u73af\u5199\u5165\u591a\u884c\u6570\u636e\uff1a<\/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(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    for line in lines:<\/p>\n<p>        file.write(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528writelines()\u65b9\u6cd5\u4e00\u6b21\u6027\u5199\u5165\u591a\u884c\u6570\u636e\uff1a<\/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(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528writelines()\u65b9\u6cd5\u5199\u5165\u591a\u884c\u6570\u636e\u65f6\uff0c\u9700\u8981\u624b\u52a8\u5728\u6bcf\u884c\u672b\u5c3e\u6dfb\u52a0\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\uff08\u5982\u56fe\u50cf\u3001\u97f3\u9891\u7b49\uff09\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff08&#39;b&#39;\uff09\u3002\u4f8b\u5982\uff0c\u5199\u5165\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.bin&quot;, &quot;wb&quot;) as file:<\/p>\n<p>    file.write(b&#39;\\x00\\x01\\x02\\x03&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528&#39;w&#39;\u6a21\u5f0f\u4f1a\u8986\u76d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u4f1a\u65b0\u5efa\u6587\u4ef6\uff0c\u800c&#39;a&#39;\u6a21\u5f0f\u4f1a\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5f02\u5e38\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u4e3a\u4e86\u907f\u514d\u610f\u5916\u60c5\u51b5\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>        file.write(&quot;Hello, World!&quot;)<\/p>\n<p>except IOError as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u5982\u679c\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u53d1\u751fIOError\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7\u5f02\u5e38\u5e76\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\uff0c\u800c\u4e0d\u4f1a\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5b9e\u4f8b\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u5b9e\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6587\u4ef6\u5199\u5165\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def write_to_file(filename, data):<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, &quot;w&quot;) as file:<\/p>\n<p>            file.write(data)<\/p>\n<p>        print(f&quot;Data successfully written to {filename}&quot;)<\/p>\n<p>    except IOError as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = &quot;This is a test.\\nWriting multiple lines of text.\\nPython is awesome!&quot;<\/p>\n<h2><strong>\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>write_to_file(&quot;example.txt&quot;, data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3awrite_to_file()\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u6587\u4ef6\u540d\u548c\u6570\u636e\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u6570\u636e\u5199\u5165\u6307\u5b9a\u6587\u4ef6\u3002\u5982\u679c\u5199\u5165\u64cd\u4f5c\u6210\u529f\uff0c\u4f1a\u8f93\u51fa\u6210\u529f\u4fe1\u606f\uff1b\u5982\u679c\u53d1\u751fIOError\u5f02\u5e38\uff0c\u4f1a\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8fdb\u884c\u6587\u4ef6\u5199\u5165\u64cd\u4f5c\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u6253\u5f00\u6587\u4ef6\u3001\u5199\u5165\u6570\u636e\u4ee5\u53ca\u5173\u95ed\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528open()\u51fd\u6570\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\uff0c\u5e38\u89c1\u7684\u6a21\u5f0f\u5305\u62ec\u5199\u6a21\u5f0f(&#39;w&#39;)\u3001\u8ffd\u52a0\u6a21\u5f0f(&#39;a&#39;)\u7b49\u3002\u5199\u5165\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7write()\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5b8c\u6210\u5199\u5165\u64cd\u4f5c\u540e\uff0c\u5fc5\u987b\u4f7f\u7528close()\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528with\u8bed\u53e5\u7b80\u5316\u6587\u4ef6\u64cd\u4f5c\uff0c\u907f\u514d\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\u3002\u5904\u7406\u591a\u884c\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216writelines()\u65b9\u6cd5\u3002\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff08&#39;b&#39;\uff09\u3002\u4e3a\u4e86\u907f\u514d\u610f\u5916\u60c5\u51b5\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002\u901a\u8fc7\u7efc\u5408\u5b9e\u4f8b\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6587\u4ef6\u5199\u5165\u64cd\u4f5c\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u7684\u6587\u4ef6\u64cd\u4f5c\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u6253\u5f00\u548c\u5173\u95ed\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6587\u4ef6\u64cd\u4f5c\u901a\u5e38\u901a\u8fc7\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u4f8b\u5982\u8bfb\u53d6\uff08&#39;r&#39;\uff09\u3001\u5199\u5165\uff08&#39;w&#39;\uff09\u3001\u8ffd\u52a0\uff08&#39;a&#39;\uff09\u7b49\u3002\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u52a1\u5fc5\u4f7f\u7528<code>close()<\/code>\u65b9\u6cd5\u6765\u5173\u95ed\u6587\u4ef6\uff0c\u4ee5\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;w&#39;)\nfile.write(&#39;Hello, World!&#39;)\nfile.close()\n<\/code><\/pre>\n<p>\u4e3a\u4e86\u66f4\u5b89\u5168\u548c\u7b80\u6d01\uff0c\u63a8\u8350\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\uff0c\u5b83\u4f1a\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:\n    file.write(&#39;Hello, World!&#39;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u8bfb\u53d6\u6587\u4ef6\u7684\u5185\u5bb9\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>read()<\/code>\u3001<code>readline()<\/code>\u548c<code>readlines()<\/code>\u3002<code>read()<\/code>\u53ef\u4ee5\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\uff0c<code>readline()<\/code>\u9010\u884c\u8bfb\u53d6\uff0c\u800c<code>readlines()<\/code>\u5219\u4f1a\u5c06\u6240\u6709\u884c\u4f5c\u4e3a\u4e00\u4e2a\u5217\u8868\u8fd4\u56de\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()  # \u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\n<\/code><\/pre>\n<p>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u8bfb\u53d6\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9ad8\u6548\u5730\u5904\u7406\u6587\u4ef6\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\u4e2d\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6ca1\u6709\u6743\u9650\u7b49\u3002\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6709\u6548\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    with open(&#39;example.txt&#39;, &#39;r&#39;) as file:\n        content = file.read()\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u6587\u4ef6\u540d\u548c\u8def\u5f84\u3002&quot;)\nexcept PermissionError:\n    print(&quot;\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u8be5\u6587\u4ef6\u3002&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u53ef\u4ee5\u63d0\u4f9b\u9002\u5f53\u7684\u53cd\u9988\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001Python\u5199\u6587\u4ef6\u64cd\u4f5c\u7cfb\u7edf\u7684\u65b9\u6cd5 \u4f7f\u7528open()\u51fd\u6570\u3001\u5199\u5165\u64cd\u4f5c\u3001\u5173\u95ed\u6587\u4ef6\uff0c\u5728Python\u4e2d\uff0c\u5199\u6587\u4ef6\u64cd\u4f5c\u7cfb [&hellip;]","protected":false},"author":3,"featured_media":1091788,"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\/1091772"}],"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=1091772"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091772\/revisions"}],"predecessor-version":[{"id":1091793,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091772\/revisions\/1091793"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1091788"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1091772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1091772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1091772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}