{"id":171212,"date":"2023-08-04T03:15:15","date_gmt":"2023-08-03T19:15:15","guid":{"rendered":"http:\/\/www.idc.net\/help\/171212\/"},"modified":"2023-08-04T03:15:15","modified_gmt":"2023-08-03T19:15:15","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e5%ae%9e%e7%94%a8%e6%96%b9%e6%b3%95%e4%b9%8b%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/171212\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1aPython\u5b9e\u7528\u65b9\u6cd5\u4e4b\u8bfb\u53d6\u672c\u5730\u6587\u4ef6"},"content":{"rendered":"<p style=\"line-height: 1.75em\"><span>\u5f53\u7535\u8111\u4e0a\u7684\u6587\u4ef6\u8981\u88ab\u8bfb\u53d6\u3001\u5b58\u50a8\u65f6\uff0c\u5c31\u8bf4\u660e\u6211\u4eec\u9700\u8981\u53bb\u4fdd\u5b58\u8fd9\u4efd\u6587\u4ef6\u3002\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u89e3\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u672c\u5730\u6587\u4ef6\uff0c\u5bf9\u4e8ePython\u521d\u5b66\u8005\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\u3002<\/span><\/p>\n<p style=\"text-align: center;line-height: 1.75em\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/187\/121\/285\/1603360080219939.png\" style=\"width: 378px;height: 148px\" class=\"aligncenter\"><\/p>\n<p style=\"line-height: 1.75em\"><strong><span>open() \u65b9\u6cd5<\/span><\/strong><\/p>\n<p style=\"line-height: 1.75em\"><span>Python open() \u65b9\u6cd5\u7528\u4e8e\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u6587\u4ef6\u5bf9\u8c61\uff0c\u5728\u5bf9\u6587\u4ef6\u8fdb\u884c\u5904\u7406\u8fc7\u7a0b\u90fd\u9700\u8981\u4f7f\u7528\u5230\u8fd9\u4e2a\u51fd\u6570\uff0c\u5982\u679c\u8be5\u6587\u4ef6\u65e0\u6cd5\u88ab\u6253\u5f00\uff0c\u4f1a\u629b\u51fa OSError\u3002<\/span><\/p>\n<p style=\"line-height: 1.75em\"><span>\u6ce8\u610f\uff1a\u4f7f\u7528<strong> open() <\/strong>\u65b9\u6cd5\u4e00\u5b9a\u8981\u4fdd\u8bc1\u5173\u95ed\u6587\u4ef6\u5bf9\u8c61\uff0c\u5373\u8c03\u7528<strong> close()<\/strong> \u65b9\u6cd5\u3002<\/span><\/p>\n<p style=\"line-height: 1.75em\"><span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong> &nbsp;open() <\/strong>\u51fd\u6570\u5e38\u7528\u5f62\u5f0f\u662f\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a<strong>\u6587\u4ef6\u540d(file)\u548c\u6a21\u5f0f(mode)<\/strong>\u3002<\/span><\/p>\n<p style=\"line-height: 1.75em\"><strong><span>\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b<\/span><\/strong><\/p>\n<p style=\"line-height: 1.75em\"><span>open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)<\/span><\/p>\n<p style=\"line-height: 1.75em\"><strong><span>\u53c2\u6570\u8bf4\u660e:<\/span><\/strong><\/p>\n<ul>\n<li>\n<p style=\"line-height: 1.75em\"><span>file: \u5fc5\u9700\uff0c\u6587\u4ef6\u8def\u5f84\uff08\u76f8\u5bf9\u6216\u8005\u7edd\u5bf9\u8def\u5f84\uff09<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 1.75em\"><span>mode: \u53ef\u9009\uff0c\u6587\u4ef6\u6253\u5f00\u6a21\u5f0f<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 1.75em\"><span>buffering: \u8bbe\u7f6e\u7f13\u51b2<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 1.75em\"><span>encoding: \u7f16\u7801\u65b9\u5f0f\u4e00\u822c\u4f7f\u7528utf8<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 1.75em\"><span>errors: \u62a5\u9519\u7ea7\u522b<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 1.75em\"><span>newline: \u533a\u5206\u6362\u884c\u7b26<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 1.75em\"><span>closefd: \u4f20\u5165\u7684file\u53c2\u6570\u7c7b\u578b<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 1.75em\"><span>opener:<\/span><\/p>\n<\/li>\n<\/ul>\n<p style=\"line-height: 1.75em\"><strong><span>mode\u7684\u53c2\u6570\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/span><\/strong><\/p>\n<p style=\"text-align: center;line-height: 1.75em\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/495\/735\/628\/1603360403741907.png\" class=\"aligncenter\"><\/p>\n<p style=\"line-height: 1.75em\"><span>\u6ce8\uff1a \u9ed8\u8ba4\u4e3a\u6587\u672c\u6a21\u5f0f\uff0c\u5982\u679c\u8981\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\uff0c\u52a0\u4e0a&nbsp;b&nbsp;\u3002<\/span><\/p>\n<p style=\"line-height: 1.75em\"><strong><span>file \u5bf9\u8c61<\/span><\/strong><\/p>\n<p style=\"line-height: 1.75em\"><span>file \u5bf9\u8c61\u4f7f\u7528 open \u51fd\u6570\u6765\u521b\u5efa\uff0c\u4e0b\u8868\u5217\u51fa\u4e86 file \u5bf9\u8c61\u5e38\u7528\u7684\u51fd\u6570\uff1a<\/span><\/p>\n<p style=\"text-align: center;line-height: 1.75em\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/920\/660\/209\/1603360429725824.png\" class=\"aligncenter\"><\/p>\n<p style=\"line-height: 1.75em\"><span>\u4f8b\u5982\uff0c\u901a\u8fc7\u8ffd\u52a0\u7684\u65b9\u5f0f\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u7136\u540e\u5199\u5165\u5b57\u5178\u6570\u636e\uff1a<\/span><\/p>\n<pre><p style=\"line-height: 1.75em\"><span>1 data = {\"\u5f20\u4e09\":89,\"\u674e\u56db\":108} 2 a = open(file = \"t.txt\",mode = 'a+') 3 for key,value in data.items(): 4 &nbsp; &nbsp; str1 = str(key)+\":\"+str(value)+\"\\n\" 5 &nbsp; &nbsp; a.write(str1) 6 a.close()<br><\/span><\/p><\/pre>\n<p style=\"line-height: 1.75em\"><strong><span>\u8fd0\u884c\u7ed3\u679c\uff1a<\/span><\/strong><\/p>\n<pre><p style=\"line-height: 1.75em\"><span>\u5f20\u4e09:89 \u674e\u56db:108<br><\/span><\/p><\/pre>\n<p style=\"line-height: 1.75em\"><span>\u5e0c\u671b\u672c\u6587\u6240\u8bf4\u7684python\u8bfb\u53d6\u672c\u5730\u6587\u4ef6\u65b9\u6cd5\uff0c\u80fd\u591f\u8ba9\u5927\u5bb6\u5bf9\u5b66\u4e60Python\u6709\u6240\u5e2e\u52a9\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u7535\u8111\u4e0a\u7684\u6587\u4ef6\u8981\u88ab\u8bfb\u53d6\u3001\u5b58\u50a8\u65f6\uff0c\u5c31\u8bf4\u660e\u6211\u4eec\u9700\u8981\u53bb\u4fdd\u5b58\u8fd9\u4efd\u6587\u4ef6\u3002\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u89e3\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u672c\u5730\u6587\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":171213,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-171212","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/171212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=171212"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/171212\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/171213"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=171212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=171212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=171212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}