{"id":515,"date":"2019-07-05T23:20:00","date_gmt":"2019-07-05T15:20:00","guid":{"rendered":"http:\/\/note.systw.net\/note\/?p=515"},"modified":"2023-11-03T23:24:18","modified_gmt":"2023-11-03T15:24:18","slug":"python-file-read-write","status":"publish","type":"post","link":"https:\/\/systw.net\/note\/archives\/515","title":{"rendered":"Python file read write"},"content":{"rendered":"\n<p>File read and write<\/p>\n\n\n\n<p>\u8b80\u6a94<br><strong>open( &lt; filepath>, [mode])<\/strong><br>mode\u5982\u4e0b<br>r \u8b80\u53d6\uff08\u9810\u8a2d\uff09<br>w \u5beb\u5165<br>a \u9644\u52a0<br>b \u4e8c\u9032\u4f4d\u6a21\u5f0f<br>t \u6587\u5b57\u6a21\u5f0f\uff08\u9810\u8a2d\uff09<br>+ \u66f4\u65b0\u78c1\u789f\u6a94\u6848<br>U \u901a\u7528\u65b0\u884c\u6a21\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ex:<br>f= open('A.txt', 'r')<br>for line in file:<br>\u3000print line\u00a0<\/code><\/pre>\n\n\n\n<p>\u8b80\u53d6\u6574\u500b\u6587\u4ef6<br><strong>.read()\u00a0<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ex:<br>f = open('test.txt')<br>f.read()<br>'Hello nsystw.netnhappy hahan'<br>f.read()<\/code><\/pre>\n\n\n\n<p>ps:<br>byte = f.read(1) \u8868\u793a\u5f9e\u6307\u6a19\u958b\u59cb\u7684\u5730\u65b9\u8b80\u53d61byte\u7684\u5167\u5bb9<\/p>\n\n\n\n<p>\u4e00\u884c\u4e00\u884c\u8b80\u53d6\u6587\u4ef6<br><strong>.readlines()\u00a0<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ex:<br>f = open('A.txt', 'r')<br>for line in f.readlines():<br>\u3000linedata = line.strip()<br>\u3000print linedata<\/code><\/pre>\n\n\n\n<p>ex:<br>\u8f38\u51fa\u7b2c3\u884c<br>f = open(&#8216;test.csv&#8217;)<br>print f.readlines()[2]<\/p>\n\n\n\n<p>ex:<br>\u8f38\u51fa\u7b2c2\u884c\u52304\u884c<br>f = open(&#8216;test.csv&#8217;)<br>for i in f.readlines()[1:3]:<br>\u3000print i<\/p>\n\n\n\n<p>ex:<br>\u8f38\u51fa\u7b2c2\u884c\u5f8c\u8cc7\u6599<br>f = open(&#8216;test.csv&#8217;)<br>for i in f.readlines()[1:]:<br>\u3000print i<\/p>\n\n\n\n<p>ex:<br>\u8f38\u51fa\u6700\u5f8c\u4e00\u884c<br>last_line = open(\u2018test.csv&#8217;, &#8220;r&#8221;).readlines()[-1]<\/p>\n\n\n\n<p>\u5c07\u8b80\u53d6\u7684\u6307\u91dd\u6307\u5411\u958b\u982d<br><strong>.seek(pos)&nbsp;<\/strong><br>pos=0 \u5c07\u6307\u91dd\u6307\u5230\u958b\u982d<br>pos=&lt; n &gt; \u5c07\u6307\u91dd\u5f80\u5f8c\u79fbn\u500bbyte<\/p>\n\n\n\n<p>\u5beb\u6a94<br><strong>.write()<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ex:<br>f = open(\"newfile.txt\", \"w\")<br>f.write(\"This is a testn\")<br>f.write(\"And here is another linen\")<br>f.close()<\/code><\/pre>\n\n\n\n<p>\u6e05\u7a7a\u6587\u4ef6\u5167\u5bb9<br><strong>.truncate()<\/strong><br>ps:<br>\u53ea\u6709\u5728\u4ee5\u4e0b\u6a21\u5f0f\u6253\u958b\u6a94\u6848\u624d\u53ef\u57f7\u884c\u8a72\u529f\u80fd<br>&#8220;r+&#8221; , &#8220;rb+&#8221; , &#8220;w&#8221; , &#8220;wb&#8221; , &#8220;wb+&#8221;<\/p>\n\n\n\n<p>\u95dc\u6a94<br><strong>.close()<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ex:<br>f = open('A.txt', 'r')<br>f.close()<\/code><\/pre>\n\n\n\n<p><br>refer<br>http:\/\/www.pythonforbeginners.com\/files\/reading-and-writing-files-in-python<\/p>\n\n\n\n<p><br>ps:<br>\u6a21\u7d44StringIO\u4e5f\u63d0\u4f9b\u5f37\u5927\u7684\u529f\u80fd\u53ef\u4ee5\u53c3\u8003<\/p>\n","protected":false},"excerpt":{"rendered":"<p>File read and write \u8b80\u6a94open( &#038;l &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14],"tags":[],"class_list":["post-515","post","type-post","status-publish","format-standard","hentry","category-develop"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/comments?post=515"}],"version-history":[{"count":0,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/515\/revisions"}],"wp:attachment":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/media?parent=515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/categories?post=515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/tags?post=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}