{"id":88,"date":"2026-08-06T00:28:03","date_gmt":"2026-08-05T16:28:03","guid":{"rendered":"https:\/\/ltrainyard.icu\/?p=88"},"modified":"2026-08-06T00:43:57","modified_gmt":"2026-08-05T16:43:57","slug":"day-11-%e6%96%87%e4%bb%b6%e8%af%bb%e5%86%99%e4%b8%8e%e6%95%b0%e6%8d%ae%e6%8c%81%e4%b9%85%e5%8c%96%ef%bc%9aopen-%e6%a8%a1%e5%bc%8f%e3%80%81with-%e4%b8%8e%e7%bc%96%e7%a0%81","status":"publish","type":"post","link":"https:\/\/ltrainyard.icu\/index.php\/2026\/08\/06\/day-11-%e6%96%87%e4%bb%b6%e8%af%bb%e5%86%99%e4%b8%8e%e6%95%b0%e6%8d%ae%e6%8c%81%e4%b9%85%e5%8c%96%ef%bc%9aopen-%e6%a8%a1%e5%bc%8f%e3%80%81with-%e4%b8%8e%e7%bc%96%e7%a0%81\/","title":{"rendered":"| Day 11 | \u6587\u4ef6\u8bfb\u5199\u4e0e\u6570\u636e\u6301\u4e45\u5316\uff1aopen \u6a21\u5f0f\u3001with \u4e0e\u7f16\u7801 |"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1 \u6570\u636e\u6301\u4e45\u5316 \u2014\u2014 \u7a0b\u5e8f\u4e3a\u4ec0\u4e48\u4f1a\u300c\u5931\u5fc6\u300d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6570\u636e\u6301\u4e45\u5316<\/strong>\uff08persistence\uff09\u5c31\u662f\u628a\u5185\u5b58\u91cc\u7684\u6570\u636e<strong>\u4fdd\u5b58\u5230\u786c\u76d8<\/strong>\uff0c\u8ba9\u7a0b\u5e8f\u7ed3\u675f\u540e\u6570\u636e\u4f9d\u7136\u5b58\u5728\u3002\u53d8\u91cf\u3001\u5217\u8868\u5b58\u5728\u5185\u5b58\u91cc\uff0c\u7a0b\u5e8f\u4e00\u5173\u5c31\u6ca1\u4e86\uff1b\u6587\u4ef6\u5b58\u5728\u786c\u76d8\u4e0a\uff0c\u4e0b\u6b21\u8fd0\u884c\u8fd8\u80fd\u8bfb\u56de\u6765\u3002\u5b66\u6587\u4ef6\u8bfb\u5199\uff0c\u672c\u8d28\u5c31\u662f\u5b66\u300c\u628a\u5185\u5b58 \u2194 \u786c\u76d8\u4e24\u5934\u6253\u901a\u300d\u3002\u4ece\u4eca\u5929\u5f00\u59cb\uff0c\u7a0b\u5e8f\u4e0d\u518d\u300c\u5931\u5fc6\u300d\u4e86\u2014\u2014\u4fdd\u5b58\u914d\u7f6e\u3001\u8bb0\u5f55\u65e5\u5fd7\u3001\u5b58\u50a8\u7528\u6237\u6570\u636e\uff0c\u90fd\u79bb\u4e0d\u5f00\u6587\u4ef6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u53d8\u91cf\u5728<strong>\u5185\u5b58<\/strong>\uff08RAM\uff09\u91cc\uff0c\u65ad\u7535\u5373\u5931\uff1b\u6587\u4ef6\u5728<strong>\u786c\u76d8<\/strong>\uff08\u78c1\u76d8\uff09\u4e0a\uff0c\u6301\u4e45\u4fdd\u5b58\u3002\u4e3a\u4e86\u6027\u80fd\uff0cPython \u7684 <code>write()<\/code> \u901a\u5e38\u5148\u5199\u5165<strong>\u7f13\u51b2\u533a<\/strong>\uff08\u5185\u5b58\u91cc\u7684\u4e00\u5757\u6682\u5b58\u533a\uff09\uff0c\u7b49\u7f13\u51b2\u533a\u6ee1\u3001\u6587\u4ef6\u5173\u95ed\u6216\u7a0b\u5e8f\u9000\u51fa\u65f6\u624d\u771f\u6b63\u5199\u8fdb\u786c\u76d8\u2014\u2014\u8fd9\u4e00\u6b65\u53eb\u300c\u51b2\u5237\u300d\uff08flush\uff09\u3002\u6240\u4ee5<strong>\u4e0d close \u6587\u4ef6\uff0c\u6570\u636e\u53ef\u80fd\u8fd8\u505c\u5728\u7f13\u51b2\u533a\u91cc\u6ca1\u843d\u76d8<\/strong>\uff0c\u8fd9\u5c31\u662f\u300cclose \u5f88\u91cd\u8981\u300d\u7684\u5e95\u5c42\u539f\u56e0\u3002\u53cd\u8fc7\u6765\uff0c<code>open(..., \"r\")<\/code> \u8bfb\u6587\u4ef6\u65f6\uff0cPython \u4ece\u786c\u76d8\u628a\u5185\u5bb9\u8bfb\u8fdb\u5185\u5b58\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a0b\u5e8f\u5185\u5b58\u50cf<strong>\u624b\u4e0a\u7684\u4fbf\u7b7e\u7eb8<\/strong>\uff1a\u5199\u5728\u4e0a\u9762\u968f\u65f6\u80fd\u770b\uff0c\u4f46\u7eb8\u4e00\u63c9\uff08\u7a0b\u5e8f\u9000\u51fa\uff09\u5185\u5bb9\u5c31\u6ca1\u4e86\u3002\u786c\u76d8\u6587\u4ef6\u50cf<strong>\u5bb6\u91cc\u7684\u7b14\u8bb0\u672c<\/strong>\uff1a\u7ffb\u5f00\uff08open\uff09\u2192 \u5199\u51e0\u7b14\uff08write\uff09\u2192 \u5408\u4e0a\uff08close\uff09\u2014\u2014\u5408\u4e0a\u8fd9\u6b65\u5f88\u91cd\u8981\uff0c\u53ea\u5199\u4e0d\u5408\uff08\u4e0d close\uff09\uff0c\u58a8\u6c34\u8fd8\u505c\u5728\u7b14\u5c16\uff08\u7f13\u51b2\u533a\uff09\uff0c\u672c\u5b50\u4e0a\u53ef\u80fd\u4ec0\u4e48\u90fd\u6ca1\u7559\u4e0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u4e0d close \u7684\u540e\u679c\u2014\u2014\u7f13\u51b2\u533a\u6ca1\u51b2\u5237\uff0c\u6587\u4ef6\u53ef\u80fd\u662f\u7a7a\u7684<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f = open(\"note2.txt\", \"w\")\nf.write(\"\u8fd9\u884c\u5185\u5bb9\u53ef\u80fd\u6ca1\u5199\u8fdb\u53bb\\n\")\n# \u5fd8\u8bb0\u8c03\u7528 f.close()\uff01\u7a0b\u5e8f\u7ed3\u675f\u65f6\u624d\u7531\u7cfb\u7edf\u56de\u6536\n\nf2 = open(\"note2.txt\", \"r\")\nprint(\"\u8bfb\u5230\u7684\u5185\u5bb9\uff1a\", repr(f2.read()))\nf2.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff08\u7f13\u51b2\u533a\u672a\u51b2\u5237\u65f6\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8bfb\u5230\u7684\u5185\u5bb9\uff1a ''\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>repr()<\/code> \u628a\u5b57\u7b26\u4e32\u7684\u300c\u771f\u5b9e\u6837\u5b50\u300d\u663e\u793a\u51fa\u6765\uff08\u7a7a\u4e32\u663e\u793a\u4e3a <code>''<\/code>\uff09\u3002\u5199\u5b8c<strong>\u6ca1\u6709 close<\/strong>\uff0c\u7f13\u51b2\u533a\u6ca1\u88ab\u51b2\u5237\uff0c\u7acb\u523b\u8bfb\u53ef\u80fd\u8bfb\u5230\u7a7a\u5185\u5bb9\u2014\u2014<strong>\u4e0d\u80fd\u4f9d\u8d56\u8fd0\u6c14<\/strong>\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u540e\u9762\u8981\u5b66 <code>with<\/code>\u2014\u2014\u5b83\u4fdd\u8bc1\u4e00\u5b9a\u4f1a\u5173\u95ed\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u8de8\u300c\u7a0b\u5e8f\u8fd0\u884c\u300d\u7684\u6301\u4e45\u5316\u2014\u2014\u5148\u5199\u3001\u518d\u5173\u3001\u518d\u8bfb<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"note3.txt\", \"w\", encoding=\"utf-8\") as f:\n    f.write(\"\u4eca\u5929\u662f\u5b66\u4e60 Python \u7684\u7b2c 11 \u5929\\n\")\n\n# \u6a21\u62df\u300c\u7b2c\u4e8c\u6b21\u8fd0\u884c\u7a0b\u5e8f\u300d\uff1a\u91cd\u65b0\u6253\u5f00\u6587\u4ef6\u8bfb\u53d6\nwith open(\"note3.txt\", \"r\", encoding=\"utf-8\") as f:\n    content = f.read()\nprint(\"\u8bfb\u56de\u6765\u7684\u5185\u5bb9\uff1a\", content, end=\"\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8bfb\u56de\u6765\u7684\u5185\u5bb9\uff1a \u4eca\u5929\u662f\u5b66\u4e60 Python \u7684\u7b2c 11 \u5929\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5185\u5bb9\u80fd\u5b8c\u6574\u8bfb\u56de\u6765\uff0c\u8bf4\u660e\u6570\u636e\u5df2\u7ecf\u4ece\u5185\u5b58\u300c\u642c\u300d\u5230\u4e86\u786c\u76d8\u2014\u2014\u8fd9\u5c31\u662f\u6301\u4e45\u5316\u3002<strong>\u4e4b\u540e\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u90fd\u80fd\u8bfb\u5230\u4e0a\u6b21\u5b58\u7684\u6570\u636e<\/strong>\uff0c\u7a0b\u5e8f\u4e0d\u518d\u5931\u5fc6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5199\u5b8c\u4e0d\u8c03\u7528 <code>f.close()<\/code> \u2192 \u95ee\u9898\uff1a\u6570\u636e\u53ef\u80fd\u505c\u5728\u7f13\u51b2\u533a\u6ca1\u843d\u76d8\uff0c\u7a0b\u5e8f\u5f02\u5e38\u9000\u51fa\u65f6\u4f1a\u4e22\u5931 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528\u5b8c\u7acb\u523b <code>close()<\/code>\uff0c\u6216\u76f4\u63a5\u7528 <code>with<\/code> \u81ea\u52a8\u5173\u95ed\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u4ee5\u4e3a <code>write()<\/code> \u662f\u300c\u5199\u5230\u786c\u76d8\u300d \u2192 \u95ee\u9898\uff1awrite \u5148\u5199\u7f13\u51b2\u533a\uff0c\u843d\u76d8\u65f6\u673a\u7531 Python \u63a7\u5236 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u9700\u8981\u7acb\u523b\u843d\u76d8\u53ef <code>f.flush()<\/code>\uff0c\u4f46\u6b63\u5e38\u5199\u5b8c close \u5373\u53ef\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5728 <code>with<\/code> \u5757\u5916\u9762\u7ee7\u7eed\u7528 f \u8bfb\u5199 \u2192 \u95ee\u9898\uff1a\u6587\u4ef6\u5df2\u5173\u95ed\uff0c\u62a5 <code>ValueError: I\/O operation on closed file<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6587\u4ef6\u64cd\u4f5c\u5168\u90e8\u5199\u5728 with \u5757<strong>\u5185\u90e8<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u5185\u5b58\u662f\u4fbf\u7b7e\uff0c\u786c\u76d8\u662f\u7b14\u8bb0\uff1bwrite \u8fdb\u7f13\u51b2\uff0cclose \u624d\u843d\u76d8\uff1b\u4e0d\u5173\u4f1a\u5931\u5fc6\uff0cwith \u6700\u7701\u5fc3\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 open() \u7684\u6a21\u5f0f \u2014\u2014 r \/ w \/ a \u4e09\u79cd\u6253\u5f00\u65b9\u5f0f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>open(\u6587\u4ef6\u540d, \u6a21\u5f0f)<\/code> \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u51b3\u5b9a<strong>\u6253\u5f00\u65b9\u5f0f<\/strong>\uff1a<code>\"r\"<\/code> \u53ea\u8bfb\uff08\u9ed8\u8ba4\uff09\u3001<code>\"w\"<\/code> \u6e05\u7a7a\u91cd\u5199\u3001<code>\"a\"<\/code> \u672b\u5c3e\u8ffd\u52a0\u3002\u9009\u9519\u6a21\u5f0f\u662f\u6587\u4ef6\u64cd\u4f5c\u6700\u5e38\u89c1\u7684 bug\uff1a\u7528 <code>w<\/code> \u6253\u5f00\u65e7\u6587\u4ef6\u4f1a<strong>\u628a\u539f\u6709\u5185\u5bb9\u6e05\u7a7a<\/strong>\u2014\u2014\u5f88\u591a\u65b0\u624b\u56e0\u6b64\u5f04\u4e22\u8fc7\u6570\u636e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6a21\u5f0f\u5b57\u7b26\u4e32\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf\u300c\u600e\u4e48\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\u53e5\u67c4\u300d\uff1a<code>r<\/code>\uff08read\uff09\u8981\u6c42\u6587\u4ef6<strong>\u5fc5\u987b\u5b58\u5728<\/strong>\uff0c\u6253\u5f00\u540e\u6e38\u6807\uff08\u8bfb\u5199\u4f4d\u7f6e\u6307\u9488\uff09\u5728\u6587\u4ef6\u5f00\u5934\uff0c\u53ea\u80fd\u8bfb\uff1b<code>w<\/code>\uff08write\uff09<strong>\u5b58\u5728\u5c31\u6e05\u7a7a<\/strong>\u3001\u4e0d\u5b58\u5728\u5c31\u65b0\u5efa\uff0c\u6253\u5f00\u540e\u6e38\u6807\u5728\u5f00\u5934\uff0c\u53ea\u80fd\u5199\uff1b<code>a<\/code>\uff08append\uff09<strong>\u4e0d\u6e05\u7a7a<\/strong>\u3001\u4e0d\u5b58\u5728\u5c31\u65b0\u5efa\uff0c\u6253\u5f00\u540e\u6e38\u6807<strong>\u81ea\u52a8\u79fb\u5230\u6587\u4ef6\u672b\u5c3e<\/strong>\uff0c\u65b0\u5185\u5bb9\u63a5\u5728\u540e\u9762\u3002\u8fd8\u6709 <code>r+<\/code>\uff08\u53ef\u8bfb\u53ef\u5199\uff0c\u4e0d\u6e05\u7a7a\uff09\u3001<code>x<\/code>\uff08\u65b0\u5efa\u4e13\u5c5e\uff0c\u5df2\u5b58\u5728\u5219\u62a5\u9519\uff09\u7b49\u53d8\u4f53\uff0c\u65b0\u624b\u5148\u638c\u63e1 r \/ w \/ a \u5c31\u591f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e09\u79cd\u6a21\u5f0f\u50cf\u4e09\u79cd<strong>\u4f7f\u7528\u7b14\u8bb0\u672c\u7684\u65b9\u5f0f<\/strong>\uff1a<code>r<\/code> \u662f\u300c\u53ea\u8bfb\u300d\u2014\u2014\u7ffb\u5f00\u53ea\u80fd\u770b\uff0c\u800c\u4e14\u672c\u5b50\u5fc5\u987b\u5b58\u5728\uff1b<code>w<\/code> \u662f\u300c\u4e70\u65b0\u672c\u5b50\u4ece\u5934\u5199\u300d\u2014\u2014\u65e7\u672c\u5b50\u76f4\u63a5\u6254\u6389\uff08\u6e05\u7a7a\uff09\uff0c\u4ece\u7b2c\u4e00\u9875\u5199\u8d77\uff1b<code>a<\/code> \u662f\u300c\u5728\u65e7\u672c\u5b50\u672b\u5c3e\u63a5\u7740\u5199\u300d\u2014\u2014\u524d\u9762\u5199\u8fc7\u7684\u5185\u5bb9\u539f\u6837\u4fdd\u7559\uff0c\u7ffb\u5230\u6700\u540e\u4e00\u9875\u5f80\u540e\u5199\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u6a21\u5f0f\u53ef\u4ee5\u5e26\u540e\u7f00\u2014\u2014&#8221;wb&#8221; \u4e8c\u8fdb\u5236\u5199\u5165\u3001&#8221;rb&#8221; \u4e8c\u8fdb\u5236\u8bfb\u53d6<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.bin\", \"wb\") as f:\n    f.write(bytes(&#91;0, 1, 2, 255]))    # \u5199 4 \u4e2a\u539f\u59cb\u5b57\u8282\n\nwith open(\"data.bin\", \"rb\") as f:\n    print(list(f.read()))             # \u8bfb\u56de\u5e76\u8f6c\u6210\u5217\u8868\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;0, 1, 2, 255]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>b<\/code> \u8868\u793a<strong>\u4e8c\u8fdb\u5236\u6a21\u5f0f<\/strong>\uff0c\u8bfb\u5199\u7684\u662f\u5b57\u8282\uff08bytes\uff09\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u2014\u2014\u56fe\u7247\u3001\u97f3\u9891\u3001\u89c6\u9891\u7b49\u975e\u6587\u672c\u6587\u4ef6\u5fc5\u987b\u7528\u4e8c\u8fdb\u5236\u6a21\u5f0f\u3002\u6587\u672c\u7528 <code>\"w\"<\/code> \/ <code>\"r\"<\/code>\uff0c\u4e8c\u8fdb\u5236\u7528 <code>\"wb\"<\/code> \/ <code>\"rb\"<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u5199\u6a21\u5f0f w \u4f1a\u6e05\u7a7a\u539f\u6709\u5185\u5bb9<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"notes.txt\", \"w\") as f:\n    f.write(\"\u7b2c\u4e00\u6b21\u5199\u5165\\n\")\nwith open(\"notes.txt\", \"w\") as f:\n    f.write(\"\u7b2c\u4e8c\u6b21\u5199\u5165\\n\")   # \u65e7\u5185\u5bb9\u88ab\u6e05\u7a7a\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7b2c\u4e8c\u6b21\u5199\u5165\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>\u8ffd\u52a0\u6a21\u5f0f a \u4e0d\u6e05\u7a7a\uff0c\u65b0\u5185\u5bb9\u5199\u5728\u6587\u4ef6\u672b\u5c3e<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"notes.txt\", \"a\") as f:\n    f.write(\"\u8ffd\u52a0\u7684\u5185\u5bb9\\n\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7b2c\u4e8c\u6b21\u5199\u5165\n\u8ffd\u52a0\u7684\u5185\u5bb9\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9009\u62e9\u53e3\u8bc0<\/strong>\uff1a\u4ece\u5934\u91cd\u5199\u3001\u8986\u76d6\u65e7\u6570\u636e \u2192 \u7528 <code>w<\/code>\uff1b\u4fdd\u7559\u65e7\u6570\u636e\u3001\u5f80\u540e\u8ffd\u52a0 \u2192 \u7528 <code>a<\/code>\uff1b\u53ea\u8bfb\u4e0d\u5199 \u2192 \u7528 <code>r<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u7528 <code>w<\/code> \u6253\u5f00\u5df2\u6709\u91cd\u8981\u6587\u4ef6 \u2192 \u95ee\u9898\uff1a\u65e7\u5185\u5bb9\u77ac\u95f4\u88ab\u6e05\u7a7a\uff0c\u65e0\u6cd5\u6062\u590d \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u8ffd\u52a0\u7528 <code>a<\/code>\uff1b\u786e\u9700\u8986\u76d6\u524d\u5148\u5907\u4efd\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u6587\u672c\u6587\u4ef6\u7528 <code>\"wb\"<\/code> \u5199\u5b57\u7b26\u4e32 \u2192 \u95ee\u9898\uff1a\u62a5 <code>TypeError: a bytes-like object is required<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6587\u672c\u7528 <code>\"w\"<\/code>\uff1b\u4e8c\u8fdb\u5236\u624d\u7528 <code>\"wb\"<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u4ee5\u4e3a <code>\"a\"<\/code> \u4e5f\u662f\u300c\u4ece\u5934\u5199\u300d \u2192 \u95ee\u9898\uff1a\u6e38\u6807\u5728\u672b\u5c3e\uff0c\u65b0\u5185\u5bb9\u6c38\u8fdc\u63a5\u5728\u6700\u540e \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u60f3\u4ece\u5934\u91cd\u5199\u7528 <code>\"w\"<\/code>\uff08\u6ce8\u610f\u4f1a\u6e05\u7a7a\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1ar \u53ea\u8bfb\u3001w \u6e05\u7a7a\u91cd\u5199\u3001a \u672b\u5c3e\u8ffd\u52a0\uff1b\u7528 w \u5148\u60f3\u6e05\u7a7a\uff0c\u4e8c\u8fdb\u5236\u52a0\u4e2a b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 with \u8bed\u53e5 \u2014\u2014 \u81ea\u52a8\u5173\u95ed\u7684\u300c\u4fdd\u9669\u7bb1\u300d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>with open(...) as f:<\/code> \u662f\u6253\u5f00\u6587\u4ef6\u7684<strong>\u6807\u51c6\u59ff\u52bf<\/strong>\uff1a\u4ee3\u7801\u5757\u4e00\u7ed3\u675f\uff0c\u6587\u4ef6\u81ea\u52a8\u5173\u95ed\u2014\u2014\u4e0d\u7ba1\u6b63\u5e38\u7ed3\u675f\u8fd8\u662f\u4e2d\u9014\u62a5\u9519\u3002\u624b\u52a8 <code>close()<\/code> \u5bb9\u6613\u5fd8\u3001\u62a5\u9519\u65f6\u8fd8\u6267\u884c\u4e0d\u5230\uff1b<code>with<\/code> \u4e00\u52b3\u6c38\u9038\u3002\u4ece\u4eca\u5929\u8d77\uff0c\u5199\u6587\u4ef6\u4ee3\u7801\u9ed8\u8ba4\u90fd\u7528 <code>with<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>with<\/code> \u8bed\u53e5\u4f9d\u8d56<strong>\u4e0a\u4e0b\u6587\u7ba1\u7406\u534f\u8bae<\/strong>\uff08context manager protocol\uff09\uff1a<code>open()<\/code> \u8fd4\u56de\u7684\u5bf9\u8c61\u5b9e\u73b0\u4e86 <code>__enter__<\/code> \u548c <code>__exit__<\/code> \u4e24\u4e2a\u7279\u6b8a\u65b9\u6cd5\u3002\u8fdb\u5165 <code>with<\/code> \u5757\u65f6\u8c03\u7528 <code>__enter__<\/code>\uff08\u8fd4\u56de\u6587\u4ef6\u5bf9\u8c61\u7ed9 as \u540e\u9762\u7684 f\uff09\uff1b<strong>\u65e0\u8bba\u5757\u5185\u4ee3\u7801\u6b63\u5e38\u8dd1\u5b8c\u8fd8\u662f\u629b\u51fa\u5f02\u5e38<\/strong>\uff0c\u9000\u51fa\u65f6\u90fd\u4f1a\u8c03\u7528 <code>__exit__<\/code>\u2014\u2014\u6587\u4ef6\u5728 <code>__exit__<\/code> \u91cc\u88ab\u5173\u95ed\u3002\u6240\u4ee5 with \u4fdd\u8bc1\u300c\u4e00\u5b9a\u6709\u59cb\u6709\u7ec8\u300d\uff0c\u8fd9\u662f\u624b\u52a8 close \u6c38\u8fdc\u505a\u4e0d\u5230\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>with<\/code> \u50cf<strong>\u9152\u5e97\u7684\u81ea\u52a8\u9000\u623f<\/strong>\uff1a\u4f60\u5165\u4f4f\u65f6\uff08<code>__enter__<\/code>\uff09\u62ff\u5230\u623f\u5361\uff08\u6587\u4ef6\u5bf9\u8c61\uff09\uff1b\u4e0d\u7ba1\u4f60\u662f\u6b63\u5e38\u9000\u623f\uff08\u4ee3\u7801\u8dd1\u5b8c\uff09\u8fd8\u662f\u88ab\u4fdd\u5b89\u67b6\u8d70\uff08\u62a5\u5f02\u5e38\uff09\uff0c\u524d\u53f0\u90fd\u4f1a\u81ea\u52a8\u5e2e\u4f60\u9000\u623f\u9500\u5361\uff08<code>__exit__<\/code> \u5173\u95ed\u6587\u4ef6\uff09\u2014\u2014\u7edd\u4e0d\u4f1a\u51fa\u73b0\u300c\u4eba\u8d70\u4e86\u623f\u5361\u8fd8\u5728\u300d\u7684\u6cc4\u6f0f\u95ee\u9898\u3002\u624b\u52a8 close \u5219\u50cf\u300c\u8bb0\u5f97\u9000\u623f\u300d\uff1a\u5fd8\u4e86\u5c31\u9ebb\u70e6\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>with \u5757\u7ed3\u675f\u540e\uff0c\u6587\u4ef6\u786e\u5b9e\u5df2\u81ea\u52a8\u5173\u95ed<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"auto.txt\", \"w\", encoding=\"utf-8\") as f:\n    f.write(\"with \u4f1a\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\\n\")\n    print(\"\u5728 with \u5757\u5185\uff0c\u6587\u4ef6\u5f00\u7740\uff1a\", not f.closed)\n\nprint(\"\u79bb\u5f00 with \u5757\u540e\uff0c\u6587\u4ef6\u5df2\u5173\u95ed\uff1a\", f.closed)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5728 with \u5757\u5185\uff0c\u6587\u4ef6\u5f00\u7740\uff1a True\n\u79bb\u5f00 with \u5757\u540e\uff0c\u6587\u4ef6\u5df2\u5173\u95ed\uff1a True\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>f.closed<\/code> \u662f\u6587\u4ef6\u5bf9\u8c61\u7684\u5c5e\u6027\u2014\u2014True \u8868\u793a\u5df2\u5173\u95ed\u3002with \u9000\u51fa\u65f6<strong>\u81ea\u52a8<\/strong>\u8c03\u7528\u4e86\u5173\u95ed\uff0c\u4e0d\u9700\u8981\u6211\u4eec\u5199\u4e00\u884c close\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u5757\u5185\u62a5\u9519\uff0c\u6587\u4ef6\u4e5f\u4f1a\u88ab\u81ea\u52a8\u5173\u95ed\uff08\u5f02\u5e38\u5b89\u5168\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    with open(\"error.txt\", \"w\", encoding=\"utf-8\") as f:\n        f.write(\"\u5199\u4e86\u4e00\u534a\u2026\u2026\")\n        raise ValueError(\"\u6a21\u62df\u4e2d\u9014\u51fa\u9519\")\nexcept ValueError as e:\n    print(\"\u6355\u83b7\u5f02\u5e38\uff1a\", e)\n\nprint(\"\u5373\u4f7f\u51fa\u9519\uff0c\u6587\u4ef6\u4e5f\u5df2\u81ea\u52a8\u5173\u95ed\uff1a\", f.closed)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6355\u83b7\u5f02\u5e38\uff1a \u6a21\u62df\u4e2d\u9014\u51fa\u9519\n\u5373\u4f7f\u51fa\u9519\uff0c\u6587\u4ef6\u4e5f\u5df2\u81ea\u52a8\u5173\u95ed\uff1a True\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f02\u5e38\u629b\u51fa\u7684\u77ac\u95f4\uff0cwith \u7684 <code>__exit__<\/code> \u4f9d\u7136\u88ab\u8c03\u7528\uff0c\u6587\u4ef6\u5173\u95ed\uff0c\u7136\u540e\u5f02\u5e38\u624d\u5411\u5916\u4f20\u64ad\u88ab <code>except<\/code> \u63a5\u4f4f\u3002\u624b\u52a8 close \u5199\u6cd5\u5728\u8fd9\u91cc\u4f1a\u300c\u8df3\u8fc7 close \u76f4\u63a5\u629b\u5f02\u5e38\u300d\u2014\u2014\u6587\u4ef6\u5c31\u6cc4\u6f0f\u4e86\u3002\u8fd9\u5c31\u662f with \u6700\u503c\u94b1\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>with open(...)<\/code> \u5fd8\u4e86 <code>as f<\/code> \u2192 \u95ee\u9898\uff1a\u8bed\u6cd5\u9519\u8bef\uff0c\u62ff\u4e0d\u5230\u6587\u4ef6\u5bf9\u8c61 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>with open(...) as f:<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5728 with \u5757\u5916\u9762\u7528 f \u8bfb\u6587\u4ef6 \u2192 \u95ee\u9898\uff1a\u62a5 <code>ValueError: I\/O operation on closed file<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u8bfb\u5199\u4ee3\u7801\u5199\u5728 with \u5757\u5185\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u624b\u5199 <code>f = open(...)<\/code> \u540e\u5fd8\u4e86 close \u2192 \u95ee\u9898\uff1a\u6587\u4ef6\u957f\u671f\u5360\u7528\uff0c\u5176\u4ed6\u7a0b\u5e8f\u6253\u4e0d\u5f00\uff1b\u5f02\u5e38\u65f6\u76f4\u63a5\u6cc4\u6f0f \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4e00\u5f8b\u7528 <code>with<\/code> \u81ea\u52a8\u7ba1\u7406\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u591a\u4e2a\u6587\u4ef6\u5199\u4e86\u591a\u4e2a with \u5d4c\u5957 \u2192 \u95ee\u9898\uff1a\u7f29\u8fdb\u5c42\u5c42\u52a0\u6df1\uff0c\u4ee3\u7801\u96be\u770b \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>with A as x, B as y:<\/code> \u4e00\u6b21\u7ba1\u7406\u591a\u4e2a\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1awith \u6253\u5f00\u81ea\u52a8\u5173\uff0c\u6b63\u5e38\u5f02\u5e38\u90fd\u9000\u623f\uff1benter \u8fdb\u3001exit \u51fa\uff0cclose \u4ece\u6b64\u4e0d\u7528\u5199\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 \u5b57\u7b26\u7f16\u7801 \u2014\u2014 \u4e2d\u6587\u4e0d\u4e71\u7801\u7684\u79d8\u5bc6<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7f16\u7801<\/strong>\uff08encoding\uff09\u662f\u628a\u5b57\u7b26\u7ffb\u8bd1\u6210\u5b57\u8282\u7684\u300c\u5bf9\u7167\u8868\u300d\u3002\u5199\u5165\u548c\u8bfb\u53d6\u7528<strong>\u4e0d\u540c\u7684\u7f16\u7801<\/strong>\u65f6\uff0c\u4e2d\u6587\u5c31\u4f1a\u4e71\u7801\u751a\u81f3\u62a5\u9519\u3002<code>utf-8<\/code> \u662f\u56fd\u9645\u901a\u7528\u7684\u7f16\u7801\uff0c\u80fd\u540c\u65f6\u5bb9\u7eb3\u4e2d\u6587\u3001\u82f1\u6587\u3001emoji\u3002\u7ed9\u6587\u4ef6\u8bfb\u5199\u52a0 <code>encoding=\"utf-8\"<\/code>\uff0c\u662f\u4e2d\u6587\u7528\u6237\u5199\u6587\u4ef6\u4ee3\u7801\u7684\u300c\u6807\u51c6\u52a8\u4f5c\u300d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7535\u8111\u53ea\u8ba4 0 \u548c 1\u3002\u5b57\u7b26\u4e32 <code>\"\u4f60\u597d\"<\/code> \u5728\u5185\u5b58\u91cc\u662f Unicode \u5b57\u7b26\uff0c\u8981\u5b58\u8fdb\u6587\u4ef6\u5fc5\u987b<strong>\u7f16\u7801<\/strong>\uff08encode\uff09\u6210\u5b57\u8282\uff1a<code>\"\u4f60\u597d\".encode(\"utf-8\")<\/code> \u5f97\u5230 <code>b'\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd'<\/code>\uff08\u6bcf\u4e2a\u6c49\u5b57\u7ea6 3 \u5b57\u8282\uff09\u3002\u8bfb\u6587\u4ef6\u65f6\u53cd\u8fc7\u6765<strong>\u89e3\u7801<\/strong>\uff08decode\uff09\u8fd8\u539f\u6210\u5b57\u7b26\u4e32\u3002\u5982\u679c\u7528 GBK \u7f16\u7801\u5199\u5165\u3001UTF-8 \u89e3\u7801\u8bfb\u53d6\uff0c\u5b57\u8282\u5bf9\u4e0d\u4e0a\u53f7\uff0c\u5c31\u53d8\u6210\u4e71\u7801\u6216\u62a5 <code>UnicodeDecodeError<\/code>\u3002Python 3 \u7684\u9ed8\u8ba4\u7f16\u7801\u662f utf-8\uff0c\u4f46<strong>\u663e\u5f0f\u5199\u51fa\u6765<\/strong>\u66f4\u7a33\u59a5\u2014\u2014\u5c24\u5176\u4ee3\u7801\u8981\u5728\u522b\u4eba\u7535\u8111\u4e0a\u8dd1\u65f6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f16\u7801\u50cf<strong>\u6469\u65af\u5bc6\u7801\u7684\u5bf9\u7167\u8868<\/strong>\uff1a\u540c\u4e00\u4e32\u300c\u6ef4\u7b54\u300d\u58f0\uff0c\u7528\u82f1\u6587\u5bf9\u7167\u8868\u7ffb\u8bd1\u662f A\uff0c\u7528\u4e2d\u6587\u5bf9\u7167\u8868\u7ffb\u8bd1\u53ef\u80fd\u5b8c\u5168\u4e0d\u540c\u3002\u5199\u5165\u6587\u4ef6\u662f\u300c\u628a\u8bdd\u7ffb\u8bd1\u6210\u7535\u62a5\u7801\u300d\uff08encode\uff09\uff0c\u8bfb\u53d6\u662f\u300c\u628a\u7535\u62a5\u7801\u7ffb\u56de\u4eba\u8bdd\u300d\uff08decode\uff09\u2014\u2014<strong>\u53d1\u9001\u548c\u63a5\u6536\u5fc5\u987b\u7528\u540c\u4e00\u5f20\u8868<\/strong>\uff0c\u5426\u5219\u5c31\u662f\u4e71\u7801\u3002utf-8 \u5c31\u662f\u300c\u5168\u7403\u901a\u7528\u7684\u90a3\u5f20\u8868\u300d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u5b57\u7b26\u4e32 \u2194 \u5b57\u8282\u7684\u7f16\u7801\u89e3\u7801\uff08\u6587\u4ef6\u8bfb\u5199\u7684\u5e95\u5c42\u672c\u8d28\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"\u4f60\u597d\"\ndata = text.encode(\"utf-8\")          # \u7f16\u7801\uff1a\u5b57\u7b26\u4e32 \u2192 \u5b57\u8282\nprint(data)                          # b'\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd'\n\nback = data.decode(\"utf-8\")          # \u89e3\u7801\uff1a\u5b57\u8282 \u2192 \u5b57\u7b26\u4e32\nprint(back)                          # \u4f60\u597d\nprint(\"\u8fd8\u539f\u6210\u529f\uff1a\", back == text)     # True\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>b'\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd'\n\u4f60\u597d\n\u8fd8\u539f\u6210\u529f\uff1a True\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>encode(\"utf-8\")<\/code> \u628a\u300c\u4f60\u597d\u300d\u53d8\u6210 6 \u4e2a\u5b57\u8282\uff08\u6bcf\u4e2a\u6c49\u5b57 3 \u5b57\u8282\uff09\uff1b<code>decode(\"utf-8\")<\/code> \u539f\u6837\u8fd8\u539f\u3002<strong>\u6587\u4ef6\u5199\u5165 = \u5148 encode \u518d\u843d\u76d8\uff0c\u6587\u4ef6\u8bfb\u53d6 = \u5148\u8bfb\u76d8\u518d decode<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u7f16\u7801\u4e0d\u4e00\u81f4\u7684\u540e\u679c\u2014\u2014\u4e71\u7801\u4e0e\u62a5\u9519\uff08\u9519\u8bef\u793a\u8303\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"gbk_demo.txt\", \"w\", encoding=\"gbk\") as f:\n    f.write(\"\u4e2d\u6587\u7528 GBK \u7f16\u7801\u4fdd\u5b58\\n\")   # \u6587\u4ef6\u91cc\u5b58\u7684\u662f GBK \u5b57\u8282\n\ntry:\n    with open(\"gbk_demo.txt\", \"r\", encoding=\"utf-8\") as f:\n        print(f.read())\nexcept UnicodeDecodeError as e:\n    print(\"\u89e3\u7801\u5931\u8d25\uff1a\", e)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff08\u793a\u4f8b\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u89e3\u7801\u5931\u8d25\uff1a 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5199\u5165\u7528 gbk\u3001\u8bfb\u53d6\u7528 utf-8\uff0c\u5b57\u8282\u5bf9\u4e0d\u4e0a\uff0c\u629b <code>UnicodeDecodeError<\/code>\u3002\u5de5\u4f5c\u4e2d\u9047\u5230\u8fd9\u4e2a\u62a5\u9519\uff0c\u7b2c\u4e00\u53cd\u5e94\u5c31\u662f\u300c\u8bfb\u5199\u7f16\u7801\u4e0d\u4e00\u81f4\u300d\u3002\u89e3\u51b3\u65b9\u6848\uff1a\u7edf\u4e00 <code>encoding=\"utf-8\"<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u8bfb\u5199\u7f16\u7801\u4e0d\u4e00\u81f4\uff08\u5199 gbk\u3001\u8bfb utf-8\uff09 \u2192 \u95ee\u9898\uff1a\u4e71\u7801\u6216 <code>UnicodeDecodeError<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u8bfb\u5199\u90fd\u5199 <code>encoding=\"utf-8\"<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5199\u4e2d\u6587\u4e0d\u6307\u5b9a\u7f16\u7801\uff08Windows \u9ed8\u8ba4\u53ef\u80fd\u4e0d\u662f utf-8\uff09 \u2192 \u95ee\u9898\uff1a\u6362\u53f0\u7535\u8111\u6216\u6362\u4e2a\u7a0b\u5e8f\u8bfb\u5c31\u4e71\u7801 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5199\u4e2d\u6587\u4e00\u5f8b\u663e\u5f0f <code>encoding=\"utf-8\"<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u7528 <code>\"wb\"<\/code> \u5199\u5b57\u7b26\u4e32 \u2192 \u95ee\u9898\uff1a\u62a5 <code>TypeError: a bytes-like object is required<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6587\u672c\u7528 <code>\"w\"<\/code>\uff1b\u8981\u5199\u5b57\u8282\u5148 <code>encode<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u8bfb\u53d6\u65f6\u7528 <code>encoding=\"gbk\"<\/code> \u8bfb utf-8 \u6587\u4ef6 \u2192 \u95ee\u9898\uff1a\u4e71\u7801\u6216\u62a5\u9519 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5148\u786e\u8ba4\u6587\u4ef6\u662f\u4ec0\u4e48\u7f16\u7801\u5199\u7684\uff0c\u8bfb\u4fa7\u4fdd\u6301\u4e00\u81f4\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u5199\u524d encode\uff0c\u8bfb\u540e decode\uff0c\u4e24\u4fa7\u540c\u7f16\u7801\uff1b\u4e2d\u6587\u52a0 utf-8\uff0c\u4e71\u7801\u8fdc\u79bb\u4f60\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5 \u8bfb\u53d6\u7684\u4e09\u79cd\u59ff\u52bf \u2014\u2014 read \/ readline \/ \u9010\u884c for<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>read()<\/code> \u4e00\u6b21\u8bfb\u5168\u90e8\u3001<code>readline()<\/code> \u8bfb\u4e00\u884c\u3001<code>for line in f<\/code> \u9010\u884c\u904d\u5386\u2014\u2014\u4e09\u79cd\u65b9\u5f0f\u5404\u6709\u9002\u7528\u573a\u666f\u3002<strong>\u8bfb\u5927\u6587\u4ef6<\/strong>\uff08\u65e5\u5fd7\u3001\u51e0 GB \u7684\u6570\u636e\uff09\u65f6\u7528\u9519\u65b9\u5f0f\u4f1a\u76f4\u63a5\u628a\u5185\u5b58\u6491\u7206\uff0c\u8fd9\u662f\u6587\u4ef6\u8bfb\u53d6\u6700\u91cd\u8981\u7684\u5b9e\u6218\u6280\u5de7\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6587\u4ef6\u5bf9\u8c61\u5185\u90e8\u7ef4\u62a4\u4e00\u4e2a\u300c\u6e38\u6807\u300d\uff08\u8bfb\u5199\u4f4d\u7f6e\uff09\uff0c\u6bcf\u6b21\u8bfb\u53d6\u90fd\u4ece\u6e38\u6807\u5904\u5f00\u59cb\u3001\u8bfb\u5b8c\u79fb\u52a8\u6e38\u6807\u3002<code>read()<\/code> \u628a\u5269\u4f59\u5185\u5bb9\u4e00\u6b21\u6027\u8bfb\u6210\u5b57\u7b26\u4e32\uff08\u5927\u6587\u4ef6\u4f1a\u5360\u5927\u91cf\u5185\u5b58\uff09\uff1b<code>readline()<\/code> \u53ea\u8bfb\u5230\u4e0b\u4e00\u4e2a\u6362\u884c\u7b26\u4e3a\u6b62\uff1b<code>for line in f<\/code> \u5e95\u5c42\u662f<strong>\u60f0\u6027\u9010\u884c\u8bfb\u53d6<\/strong>\u2014\u2014\u6bcf\u5faa\u73af\u4e00\u6b21\u53ea\u8bfb\u4e00\u884c\uff0c<strong>\u4e0d\u4f1a\u628a\u6574\u4e2a\u6587\u4ef6\u88c5\u8fdb\u5185\u5b58<\/strong>\uff0c\u6240\u4ee5\u8bfb\u591a\u5927\u7684\u6587\u4ef6\u90fd\u53ea\u5360\u4e00\u884c\u7684\u5185\u5b58\u3002\u53e6\u5916\u6ce8\u610f\uff1a<code>read()<\/code> \u8bfb\u5b8c\u540e\u6e38\u6807\u5728\u6587\u4ef6\u672b\u5c3e\uff0c\u518d\u8c03\u7528 <code>read()<\/code> \u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\uff1b\u60f3\u4ece\u5934\u518d\u8bfb\uff0c\u7528 <code>f.seek(0)<\/code> \u628a\u6e38\u6807\u79fb\u56de\u5f00\u5934\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e09\u79cd\u59ff\u52bf\u50cf\u4e09\u79cd<strong>\u8bfb\u4fe1\u65b9\u5f0f<\/strong>\uff1a<code>read()<\/code> \u662f\u628a\u6574\u5c01\u4fe1\u4e00\u53e3\u6c14\u8bfb\u5b8c\uff08\u4fe1\u77ed\u53ef\u4ee5\uff0c\u4fe1\u957f\u4f1a\u7d2f\uff09\uff1b<code>readline()<\/code> \u662f\u53ea\u8bfb\u7b2c\u4e00\u884c\u5c31\u505c\uff1b<code>for line in f<\/code> \u662f<strong>\u4e00\u884c\u4e00\u884c\u5730\u8bfb<\/strong>\u2014\u2014\u8bfb\u5b8c\u4e00\u884c\u3001\u4e22\u4e00\u884c\uff0c\u51e0\u5341\u4e07\u884c\u7684\u6d41\u6c34\u8d26\u4e5f\u80fd\u8f7b\u677e\u8bfb\u5b8c\uff0c\u56e0\u4e3a\u624b\u91cc\u6c38\u8fdc\u53ea\u6709\u4e00\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>read() \u4e00\u6b21\u8bfb\u5168\u90e8 vs readline() \u8bfb\u4e00\u884c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"lines.txt\", \"r\", encoding=\"utf-8\") as f:\n    all_text = f.read()          # \u4e00\u53e3\u6c14\u8bfb\u5b8c\u5269\u4f59\u5168\u90e8\n    print(\"read() \u7ed3\u679c\uff1a\", repr(all_text))\n\nwith open(\"lines.txt\", \"r\", encoding=\"utf-8\") as f:\n    line1 = f.readline()         # \u53ea\u8bfb\u7b2c\u4e00\u884c\n    line2 = f.readline()         # \u6e38\u6807\u5df2\u4e0b\u79fb\uff0c\u518d\u8bfb\u662f\u7b2c\u4e8c\u884c\n    print(\"readline() \u4e24\u6b21\uff1a\", repr(line1), repr(line2))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>read() \u7ed3\u679c\uff1a '\u7b2c\u4e00\u884c\\n\u7b2c\u4e8c\u884c\\n\u7b2c\u4e09\u884c\\n'\nreadline() \u4e24\u6b21\uff1a '\u7b2c\u4e00\u884c\\n' '\u7b2c\u4e8c\u884c\\n'\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>repr()<\/code> \u80fd\u770b\u5230\u5b57\u7b26\u4e32\u91cc\u7684\u6362\u884c\u7b26 <code>\\n<\/code>\u3002<code>read()<\/code> \u4e00\u6b21\u62ff\u56de\u5168\u90e8\u5185\u5bb9\uff1b<code>readline()<\/code> \u6bcf\u6b21\u53ea\u62ff\u4e00\u884c\uff0c\u4e14<strong>\u6e38\u6807\u4f1a\u8bb0\u4f4f\u4f4d\u7f6e<\/strong>\u2014\u2014\u7b2c\u4e8c\u6b21\u8c03\u7528\u81ea\u7136\u8bfb\u5230\u7b2c\u4e8c\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>readlines() \u4e00\u6b21\u62ff\u56de\u6240\u6709\u884c\u7ec4\u6210\u7684\u5217\u8868<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"lines.txt\", \"r\", encoding=\"utf-8\") as f:\n    rows = f.readlines()         # \u8fd4\u56de\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u4e00\u884c\nprint(\"\u5171\u6709\", len(rows), \"\u884c\")\nprint(\"\u7b2c 2 \u884c\u662f\uff1a\", rows&#91;1], end=\"\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5171\u6709 3 \u884c\n\u7b2c 2 \u884c\u662f\uff1a \u7b2c\u4e8c\u884c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6587\u4ef6\u4e0d\u5927\u3001\u60f3\u7528\u4e0b\u6807\u8bbf\u95ee\u67d0\u4e00\u884c\u65f6\u5f88\u65b9\u4fbf\uff1b\u4f46<strong>\u5927\u6587\u4ef6\u614e\u7528<\/strong>\u2014\u2014\u6574\u4e2a\u5217\u8868\u4e5f\u5360\u5185\u5b58\uff0c\u6b64\u65f6\u7528\u9010\u884c for\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>for line in f \u9010\u884c\u904d\u5386<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f = open(\"hello.txt\", \"r\")\nfor line in f:              # \u6587\u4ef6\u5bf9\u8c61\u53ef\u4ee5\u76f4\u63a5\u7528 for \u5faa\u73af\u9010\u884c\u904d\u5386\n    print(\"\u8bfb\u5230\u4e00\u884c\uff1a\", line, end=\"\")\nf.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f <code>print(line, end=\"\")<\/code>\uff1a\u56e0\u4e3a\u6bcf\u884c\u672b\u5c3e\u672c\u6765\u5c31\u5e26\u7740\u6362\u884c\u7b26 <code>\\n<\/code>\uff0c\u5982\u679c\u518d\u7528\u9ed8\u8ba4\u7684 print \u6362\u884c\uff0c\u5c31\u4f1a\u51fa\u73b0\u7a7a\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5927\u6587\u4ef6\u7528 <code>f.read()<\/code> \u6216 <code>readlines()<\/code> \u2192 \u95ee\u9898\uff1a\u6574\u4e2a\u6587\u4ef6\u88c5\u8fdb\u5185\u5b58\uff0c\u51e0 GB \u6587\u4ef6\u76f4\u63a5\u6491\u7206 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>for line in f<\/code> \u9010\u884c\u8bfb\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>read()<\/code> \u4e4b\u540e\u518d\u8c03 <code>read()<\/code> \u60f3\u518d\u8bfb\u4e00\u904d \u2192 \u95ee\u9898\uff1a\u6e38\u6807\u5df2\u5728\u672b\u5c3e\uff0c\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5148 <code>f.seek(0)<\/code> \u79fb\u56de\u5f00\u5934\uff0c\u6216\u91cd\u65b0 open\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u7528 <code>print(line)<\/code> \u6253\u5370\u8bfb\u5230\u7684\u884c \u2192 \u95ee\u9898\uff1a\u884c\u5c3e\u81ea\u5e26 <code>\\n<\/code>\uff0cprint \u53c8\u52a0\u4e00\u4e2a\uff0c\u51fa\u73b0\u7a7a\u884c \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>print(line, end=\"\")<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u9010\u884c\u8bfb\u65f6 <code>rstrip()<\/code> \u7528\u9519\u4f4d\u7f6e \u2192 \u95ee\u9898\uff1a\u610f\u5916\u5220\u6389\u884c\u9996\u7a7a\u683c \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u53ea\u60f3\u53bb\u6389\u672b\u5c3e\u6362\u884c\u7528 <code>line.rstrip(\"\\n\")<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aread \u5168\u8bfb\u3001readline \u5355\u884c\u3001for \u9010\u884c\u6700\u7701\u5185\u5b58\uff1b\u6e38\u6807\u8bb0\u4f4d\u7f6e\uff0cseek \u56de\u5f00\u5934\uff0c\u5927\u6587\u4ef6\u7528 for\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6 \u52a8\u624b\u5b9e\u8df5\uff1a\u8bb0\u4e8b\u672c\u7a0b\u5e8f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u505a\u4e00\u4e2a\u300c\u8bb0\u4e8b\u672c\u300d\uff1a\u7528\u6237\u8f93\u5165\u591a\u884c\u5185\u5bb9\uff0c\u8f93\u5165 <code>exit<\/code> \u7ed3\u675f\uff0c\u7136\u540e\u4fdd\u5b58\u5230 <code>mynotes.txt<\/code>\u3002\u7a0b\u5e8f\u8981\u80fd\u7ee7\u7eed\u8ffd\u52a0\uff0c\u4e0d\u8986\u76d6\u65e7\u7b14\u8bb0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># my_notepad.py \u2014\u2014 \u7b80\u6613\u8bb0\u4e8b\u672c\n# \u7528\u6237\u8f93\u5165\u5185\u5bb9\uff0c\u8f93\u5165 exit \u7ed3\u675f\uff0c\u5185\u5bb9\u8ffd\u52a0\u4fdd\u5b58\u5230 mynotes.txt\n\ndef main():\n    print(\"\u7b80\u6613\u8bb0\u4e8b\u672c\uff1a\u9010\u884c\u8f93\u5165\uff0c\u8f93\u5165 exit \u7ed3\u675f\u4fdd\u5b58\")\n    lines = &#91;]\n    while True:\n        line = input(\"&gt; \")\n        if line == \"exit\":\n            break\n        lines.append(line)\n\n    if lines:\n        with open(\"mynotes.txt\", \"a\", encoding=\"utf-8\") as f:\n            for line in lines:\n                f.write(line + \"\\n\")\n        print(f\"\u5df2\u8ffd\u52a0\u4fdd\u5b58 {len(lines)} \u884c\u5230 mynotes.txt\")\n    else:\n        print(\"\u6ca1\u6709\u8f93\u5165\u4efb\u4f55\u5185\u5bb9\")\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u6548\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7b80\u6613\u8bb0\u4e8b\u672c\uff1a\u9010\u884c\u8f93\u5165\uff0c\u8f93\u5165 exit \u7ed3\u675f\u4fdd\u5b58\n&gt; \u4eca\u5929\u5b66\u4e86\u6587\u4ef6\u8bfb\u5199\n&gt; \u660e\u5929\u5b66 CSV \u548c JSON\n&gt; exit\n\u5df2\u8ffd\u52a0\u4fdd\u5b58 2 \u884c\u5230 mynotes.txt\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u8fd0\u884c\u4e00\u6b21\u8f93\u5165\u65b0\u5185\u5bb9\uff0c\u6253\u5f00 <code>mynotes.txt<\/code> \u4f1a\u53d1\u73b0\u65e7\u5185\u5bb9\u8fd8\u5728\uff0c\u65b0\u5185\u5bb9\u8ffd\u52a0\u5728\u540e\u9762\u2014\u2014\u8fd9\u5c31\u662f <code>a<\/code> \u6a21\u5f0f\u7684\u5a01\u529b\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u628a Day 4 \u7684 while\/break\u3001Day 6 \u7684\u5217\u8868 append\u3001Day 8 \u7684\u51fd\u6570\u3001Day 11 \u7684\u6587\u4ef6\u8ffd\u52a0\u5168\u90e8\u4e32\u8d77\u6765\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5347\u7ea7\u6311\u6218<\/strong>\uff1a\u8bd5\u7740\u628a\u8f93\u5165\u63d0\u793a\u6539\u6210\u300c\u663e\u793a\u5f53\u524d\u5df2\u8f93\u5165\u7684\u884c\u6570\u300d\uff0c\u5e76\u628a\u4fdd\u5b58\u8def\u5f84\u6539\u6210\u7528 <code>os.path.join<\/code> \u62fc\u5230 <code>data<\/code> \u6587\u4ef6\u5939\uff08\u5148 <code>os.makedirs<\/code> \u521b\u5efa\u76ee\u5f55\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4eca\u65e5\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6570\u636e\u6301\u4e45\u5316\uff1awrite \u5148\u8fdb\u7f13\u51b2\u533a\u3001close\uff08\u6216 with\uff09\u624d\u843d\u76d8\uff0c\u7a0b\u5e8f\u4e0d\u518d\u5931\u5fc6\n<code>open()<\/code> \u7684 <code>r<\/code> \/ <code>w<\/code> \/ <code>a<\/code> \u4e09\u79cd\u6a21\u5f0f\uff1a\u53ea\u8bfb \/ \u6e05\u7a7a\u91cd\u5199 \/ \u672b\u5c3e\u8ffd\u52a0\uff1b\u7528 <code>w<\/code> \u524d\u5148\u786e\u8ba4\u65e7\u6570\u636e\u4e0d\u8981\u4e86\n\u517b\u6210\u4e86\u7528 <code>with<\/code> \u6253\u5f00\u6587\u4ef6\u7684\u4e60\u60ef\uff0c\u4e0d\u518d\u624b\u5199 <code>close()<\/code>\uff1b\u5757\u5185\u62a5\u9519\u6587\u4ef6\u4e5f\u4f1a\u81ea\u52a8\u5173\u95ed\n\u7f16\u7801\u539f\u7406\uff1aencode \u628a\u5b57\u7b26\u7ffb\u8bd1\u6210\u5b57\u8282\u3001decode \u8fd8\u539f\uff1b\u8bfb\u5199\u4e24\u4fa7\u7f16\u7801\u5fc5\u987b\u4e00\u81f4\n\u4e2d\u6587\u6587\u4ef6\u8bfb\u5199\u90fd\u52a0\u4e86 <code>encoding=\"utf-8\"<\/code>\n\u4f1a\u7528 <code>os.path.join<\/code> \/ <code>os.path.exists<\/code> \u5904\u7406\u8def\u5f84\uff0c\u8de8\u5e73\u53f0\u4e0d\u62a5\u9519\n\u4f1a\u7528 <code>read()<\/code> \/ <code>readline()<\/code> \/ <code>for line in f<\/code> \/ <code>readlines()<\/code>\uff0c\u5927\u6587\u4ef6\u7528\u9010\u884c for\n\u8bb0\u4e8b\u672c\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u8ffd\u52a0\u4fdd\u5b58\uff0c\u6253\u5f00\u6587\u4ef6\u770b\u5230\u5185\u5bb9<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u62a5\u9519<\/th><th>\u539f\u56e0<\/th><th>\u4fee\u590d<\/th><\/tr><\/thead><tbody><tr><td><code>FileNotFoundError<\/code><\/td><td>\u7528 <code>r<\/code> \u6a21\u5f0f\u6253\u5f00\u4e0d\u5b58\u5728\u7684\u6587\u4ef6<\/td><td>\u5148 <code>os.path.exists<\/code> \u5224\u65ad\uff0c\u6216\u7528 <code>w<\/code> \/ <code>a<\/code> \u521b\u5efa<\/td><\/tr><tr><td><code>PermissionError<\/code><\/td><td>\u6587\u4ef6\u6b63\u88ab\u522b\u7684\u7a0b\u5e8f\u6253\u5f00\uff0c\u6216\u6ca1\u6709\u5199\u5165\u6743\u9650<\/td><td>\u5173\u95ed\u5360\u7528\u8be5\u6587\u4ef6\u7684\u7a0b\u5e8f\uff0c\u68c0\u67e5\u8def\u5f84\u6743\u9650<\/td><\/tr><tr><td><code>UnicodeDecodeError<\/code><\/td><td>\u8bfb\u53d6\u65f6\u7f16\u7801\u548c\u5199\u5165\u65f6\u4e0d\u4e00\u81f4\uff08\u5982 utf-8 \u5199\u7684\u7528 gbk \u8bfb\uff09<\/td><td>\u8bfb\u5199\u90fd\u663e\u5f0f\u4f20 <code>encoding=\"utf-8\"<\/code><\/td><\/tr><tr><td><code>UnicodeEncodeError<\/code><\/td><td>\u5f80\u6587\u4ef6\u5199\u4e2d\u6587\u4f46\u6ca1\u6709\u6307\u5b9a\u652f\u6301\u4e2d\u6587\u7684\u7f16\u7801<\/td><td>\u5199\u5165\u65f6\u52a0 <code>encoding=\"utf-8\"<\/code><\/td><\/tr><tr><td><code>TypeError: write() argument must be str<\/code><\/td><td>\u628a\u6570\u5b57\u3001\u5217\u8868\u76f4\u63a5\u4ea4\u7ed9 <code>write()<\/code><\/td><td>\u5148\u8f6c\u6210\u5b57\u7b26\u4e32\uff1a<code>str(\u6570\u5b57)<\/code> \u6216 <code>f\"{\u6570\u5b57}\"<\/code><\/td><\/tr><tr><td><code>ValueError: I\/O operation on closed file<\/code><\/td><td>\u5728 with \u5757\u5916 \/ close \u4e4b\u540e\u7ee7\u7eed\u8bfb\u5199\u6587\u4ef6<\/td><td>\u6587\u4ef6\u64cd\u4f5c\u5168\u90e8\u5199\u5728 with \u5757\u5185<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1 \u6570\u636e\u6301\u4e45\u5316 \u2014\u2014 \u7a0b\u5e8f\u4e3a\u4ec0\u4e48\u4f1a\u300c\u5931\u5fc6\u300d \u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f \u6570\u636e\u6301\u4e45\u5316\uff08persistence\uff09\u5c31\u662f\u628a\u5185\u5b58 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[16,9],"tags":[17],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-python","category-code-dev","tag-python-day"],"_links":{"self":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":2,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}