{"id":1009615,"date":"2024-12-27T11:13:22","date_gmt":"2024-12-27T03:13:22","guid":{"rendered":""},"modified":"2024-12-27T11:13:24","modified_gmt":"2024-12-27T03:13:24","slug":"python%e5%86%99%e6%96%87%e4%bb%b6%e5%a6%82%e4%bd%95%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1009615.html","title":{"rendered":"Python\u5199\u6587\u4ef6\u5982\u4f55\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084455\/dc0ae752-febc-4f48-8e15-e63e9a954e27.webp\" alt=\"Python\u5199\u6587\u4ef6\u5982\u4f55\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5199\u6587\u4ef6\u65f6\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u65b9\u4fbf\u5730\u5199\u5165\u591a\u884c\u6570\u636e\u6216\u91cd\u590d\u5199\u5165\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00\u6587\u4ef6\u3001\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\uff08\u5982for\u6216while\uff09\u3001\u5199\u5165\u6570\u636e\u4ee5\u53ca\u5173\u95ed\u6587\u4ef6\u6765\u5b8c\u6210\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\u8fd8\u80fd\u63d0\u9ad8\u6548\u7387\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u6253\u5f00\u6587\u4ef6\uff08\u4f7f\u7528with open\u8bed\u53e5\uff09\u3001\u5728\u5faa\u73af\u4e2d\u4f7f\u7528write\u65b9\u6cd5\u5199\u5165\u3001\u5173\u95ed\u6587\u4ef6\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e2a\u6b65\u9aa4\u53ca\u76f8\u5173\u6ce8\u610f\u4e8b\u9879\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6587\u4ef6\u6253\u5f00\u4e0e\u5173\u95ed<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u5199\u5165\u64cd\u4f5c\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u6253\u5f00\u6587\u4ef6\u3002Python\u63d0\u4f9b\u4e86open()\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u901a\u5e38\uff0c\u6211\u4eec\u4f7f\u7528with open\u8bed\u53e5\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u5173\u95ed\u7684\u95ee\u9898\uff0c\u51cf\u5c11\u8d44\u6e90\u6cc4\u9732\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5f00\u6587\u4ef6\u7684\u6a21\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>\u5728open()\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u7684\u6253\u5f00\u6a21\u5f0f\u3002\u5e38\u7528\u7684\u5199\u5165\u6a21\u5f0f\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>&#39;w&#39;\uff1a\u5199\u5165\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5219\u4f1a\u88ab\u6e05\u7a7a\uff1b\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u521b\u5efa\u65b0\u6587\u4ef6\u3002<\/li>\n<li>&#39;a&#39;\uff1a\u8ffd\u52a0\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u6570\u636e\u4f1a\u5199\u5230\u6587\u4ef6\u672b\u5c3e\uff1b\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u521b\u5efa\u65b0\u6587\u4ef6\u3002<\/li>\n<li>&#39;x&#39;\uff1a\u72ec\u5360\u521b\u5efa\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1FileExistsError\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528with open\u8bed\u53e5<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528with open\u8bed\u53e5\u80fd\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u65e0\u9700\u624b\u52a8\u8c03\u7528close()\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u5f0f\u66f4\u52a0\u5b89\u5168\u548c\u7b80\u6d01\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>    # \u6587\u4ef6\u64cd\u4f5c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u5199\u5165\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528for\u5faa\u73af\u6216while\u5faa\u73af\u6765\u5b9e\u73b0\u5bf9\u6587\u4ef6\u7684\u591a\u6b21\u5199\u5165\u64cd\u4f5c\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528for\u5faa\u73af\u5199\u5165<\/strong><\/li>\n<\/ol>\n<p><p>for\u5faa\u73af\u9002\u7528\u4e8e\u5df2\u77e5\u9700\u8981\u5199\u5165\u6b21\u6570\u6216\u9700\u8981\u904d\u5386\u7684\u5bf9\u8c61\u4e3a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u5199\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [&#39;Line 1&#39;, &#39;Line 2&#39;, &#39;Line 3&#39;]<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for line in data:<\/p>\n<p>        file.write(line + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868data\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u88ab\u5199\u5165\u5230\u4e86\u6587\u4ef6example.txt\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u5360\u636e\u4e00\u884c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528while\u5faa\u73af\u5199\u5165<\/strong><\/li>\n<\/ol>\n<p><p>while\u5faa\u73af\u9002\u7528\u4e8e\u5199\u5165\u6761\u4ef6\u672a\u77e5\u6216\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u52a8\u6001\u51b3\u5b9a\u5199\u5165\u6b21\u6570\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u6301\u7eed\u5199\u5165\u76f4\u5230\u7528\u6237\u8f93\u5165\u505c\u6b62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    line = input(&quot;Enter text (type &#39;exit&#39; to finish): &quot;)<\/p>\n<p>    while line.lower() != &#39;exit&#39;:<\/p>\n<p>        file.write(line + &#39;\\n&#39;)<\/p>\n<p>        line = input(&quot;Enter text (type &#39;exit&#39; to finish): &quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u4e0d\u65ad\u8f93\u5165\u6587\u672c\uff0c\u8f93\u5165&#39;exit&#39;\u65f6\u7ec8\u6b62\u5199\u5165\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u5199\u5165\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002\u5728\u5199\u5165\u6587\u4ef6\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5199\u5165\u5b57\u7b26\u4e32<\/strong><\/li>\n<\/ol>\n<p><p>\u5b57\u7b26\u4e32\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528write()\u65b9\u6cd5\u5199\u5165\u6587\u4ef6\uff0c\u65e0\u9700\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5199\u5165\u6570\u5b57<\/strong><\/li>\n<\/ol>\n<p><p>\u6570\u5b57\u9700\u8981\u5148\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u624d\u80fd\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(str(number) + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5199\u5165\u5217\u8868\u6216\u5b57\u5178<\/strong><\/li>\n<\/ol>\n<p><p>\u5217\u8868\u6216\u5b57\u5178\u9700\u8981\u5148\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528str()\u51fd\u6570\u76f4\u63a5\u8f6c\u6362\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528json\u6a21\u5757\u7684dumps()\u65b9\u6cd5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(json.dumps(data) + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5f02\u5e38\u5904\u7406\u4e0e\u6587\u4ef6\u5199\u5165<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u5199\u5165\u64cd\u4f5c\u65f6\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\u4e3a\u4e86\u63d0\u9ad8\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6355\u83b7\u5f02\u5e38<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528try-except\u7ed3\u6784\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u672a\u5904\u7406\u7684\u5f02\u5e38\u800c\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>        file.write(&quot;Hello, World!\\n&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<ol start=\"2\">\n<li><strong>\u5e38\u89c1\u5f02\u5e38\u7c7b\u578b<\/strong><\/li>\n<\/ol>\n<ul>\n<li>IOError\uff1a\u8f93\u5165\u8f93\u51fa\u64cd\u4f5c\u5931\u8d25\u3002<\/li>\n<li>FileNotFoundError\uff1a\u5c1d\u8bd5\u6253\u5f00\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\u3002<\/li>\n<li>PermissionError\uff1a\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><p>\u4e94\u3001\u6027\u80fd\u4f18\u5316\u4e0e\u6587\u4ef6\u5199\u5165<\/p>\n<\/p>\n<p><p>\u5f53\u5904\u7406\u5927\u6587\u4ef6\u6216\u9700\u8981\u8fdb\u884c\u5927\u91cf\u5199\u5165\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6027\u80fd\u74f6\u9888\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6027\u80fd\u4f18\u5316\u5efa\u8bae\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u51cf\u5c11\u78c1\u76d8\u5199\u5165\u6b21\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u5c3d\u91cf\u5c06\u591a\u4e2a\u5199\u5165\u64cd\u4f5c\u5408\u5e76\u4e3a\u4e00\u6b21\uff0c\u4ee5\u51cf\u5c11\u78c1\u76d8\u5199\u5165\u6b21\u6570\uff0c\u63d0\u9ad8\u6548\u7387\u3002\u4f8b\u5982\uff0c\u4f7f\u7528join()\u65b9\u6cd5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u518d\u5199\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&#39;Line 1&#39;, &#39;Line 2&#39;, &#39;Line 3&#39;]<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;\\n&#39;.join(lines) + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u7f13\u51b2\u533a<\/strong><\/li>\n<\/ol>\n<p><p>Python\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\u9ed8\u8ba4\u4f7f\u7528\u7f13\u51b2\u533a\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6548\u7387\u3002\u5728open()\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7buffering\u53c2\u6570\u624b\u52a8\u8bbe\u7f6e\u7f13\u51b2\u533a\u5927\u5c0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;, buffering=2048) as file:<\/p>\n<p>    file.write(&quot;Hello, World!\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u8fdb\u884c\u6587\u4ef6\u5199\u5165\u64cd\u4f5c\u65f6\uff0c\u638c\u63e1\u6587\u4ef6\u7684\u6253\u5f00\u4e0e\u5173\u95ed\u3001\u5faa\u73af\u5199\u5165\u6570\u636e\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u6570\u636e\u3001\u5f02\u5e38\u5904\u7406\u4ee5\u53ca\u6027\u80fd\u4f18\u5316\u7b49\u65b9\u9762\u7684\u6280\u5de7\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u3001\u66f4\u5b89\u5168\u5730\u5b8c\u6210\u6587\u4ef6\u64cd\u4f5c\u4efb\u52a1\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u5904\u7406\u6587\u4ef6\u65f6\u65e2\u5177\u5907\u826f\u597d\u7684\u6027\u80fd\uff0c\u53c8\u5177\u5907\u8db3\u591f\u7684\u9c81\u68d2\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5faa\u73af\u5199\u5165\u591a\u4e2a\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u521b\u5efa\u548c\u5199\u5165\u591a\u4e2a\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u7ed3\u5408<code>open()<\/code>\u51fd\u6570\u548c<code>write()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u8981\u521b\u5efa\u7684\u6587\u4ef6\u540d\u548c\u5185\u5bb9\uff0c\u7136\u540e\u5728\u5faa\u73af\u4e2d\u4f9d\u6b21\u6253\u5f00\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(5):\n    with open(f&#39;file_{i}.txt&#39;, &#39;w&#39;) as f:\n        f.write(f&#39;This is file number {i}.&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u521b\u5efa5\u4e2a\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u4e3a<code>file_0.txt<\/code>\u5230<code>file_4.txt<\/code>\uff0c\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u5199\u5165\u76f8\u5e94\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5faa\u73af\u4e2d\u8ffd\u52a0\u5185\u5bb9\u5230\u4e00\u4e2a\u6587\u4ef6\uff1f<\/strong><br \/>\u82e5\u5e0c\u671b\u5728\u5faa\u73af\u4e2d\u5411\u540c\u4e00\u4e2a\u6587\u4ef6\u8ffd\u52a0\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>&#39;a&#39;<\/code>\u6a21\u5f0f\u6765\u6253\u5f00\u6587\u4ef6\u3002\u8fd9\u5c06\u5141\u8bb8\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u65b0\u5185\u5bb9\uff0c\u800c\u4e0d\u4f1a\u8986\u76d6\u73b0\u6709\u5185\u5bb9\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;a&#39;) as f:\n    for i in range(3):\n        f.write(f&#39;Appending line {i}\\n&#39;)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5728<code>output.txt<\/code>\u6587\u4ef6\u4e2d\u8ffd\u52a0\u4e09\u884c\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5faa\u73af\u5199\u6587\u4ef6\u65f6\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6587\u4ef6\u5199\u5165\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\uff0c\u5982\u6587\u4ef6\u6743\u9650\u95ee\u9898\u6216\u78c1\u76d8\u7a7a\u95f4\u4e0d\u8db3\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u6f5c\u5728\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(5):\n    try:\n        with open(f&#39;file_{i}.txt&#39;, &#39;w&#39;) as f:\n            f.write(f&#39;This is file number {i}.&#39;)\n    except IOError as e:\n        print(f&#39;Error writing to file file_{i}.txt: {e}&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u5199\u6587\u4ef6\u7684\u8fc7\u7a0b\u4e2d\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u5e76\u53ca\u65f6\u5904\u7406\u9519\u8bef\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5199\u6587\u4ef6\u65f6\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u65b9\u4fbf\u5730\u5199\u5165\u591a\u884c\u6570\u636e\u6216\u91cd\u590d\u5199\u5165\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00\u6587\u4ef6\u3001\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\uff08\u5982f [&hellip;]","protected":false},"author":3,"featured_media":1009624,"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\/1009615"}],"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=1009615"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009615\/revisions"}],"predecessor-version":[{"id":1009626,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009615\/revisions\/1009626"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1009624"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1009615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1009615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1009615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}