{"id":1604,"date":"2011-05-03T21:32:39","date_gmt":"2011-05-03T20:32:39","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1604"},"modified":"2011-05-03T21:32:39","modified_gmt":"2011-05-03T20:32:39","slug":"read-write","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2011\/05\/03\/read-write\/","title":{"rendered":"Read \/ Write"},"content":{"rendered":"<p>Here is how to open and read\/write to a file within Python.  Most languages supply a basic file opening and read\/writing methods with the closing method, and Python is no different which makes going from one language to another very easy (you just need to learn the classes that are capable to doing the interesting stuff \ud83d\ude42 ).<\/p>\n<p>To write to a file, you have to open it first within a write mode (&#8216;w&#8217;) and then write to that file *handler*, in the code below the variable &#8216;f&#8217; is the file *handler*<\/p>\n<pre lang=\"python\">\r\nf = open('write.txt','w');\r\nf.write('hi there\\n');\r\nf.write('this is genux\\n');\r\nf.write('and my site is codingfriends.com\\n');\r\nf.write('pyhon does OO\\n');\r\nf.close();\r\nprint 'File has been written\\n';\r\n<\/pre>\n<p>which will write to the &#8220;write.txt&#8221; the following text<\/p>\n<pre lang=\"bash\">\r\nhi there\r\nthis is genux\r\nand my site is codingfriends.com\r\npyhon does OO\r\n<\/pre>\n<p>and then to read in the file you use the read mode &#8216;r&#8217;, you are able to use the Iterator style of coding with using a &#8216;for <variable name> in <iterator variable>:&#8217;  or if you wish to read one line at a time you can use the &#8216;readline()&#8217; method, and to seek within a file you use the method &#8216;seek(<place within file>)&#8217;.<\/p>\n<pre lang=\"python\">\r\nf = open('write.txt','r');\r\nfor readInLine in f:\r\n\tprint readInLine;\r\n\r\nprint 'seek to the start of the file';\r\nf.seek(0);\r\nanotherLine = f.readline();\r\nprint anotherLine;\r\n\r\nf.close();\r\n<\/pre>\n<p>and here is the output<\/p>\n<pre lang=\"bash\">\r\nhi there\r\n\r\nthis is genux\r\n\r\nand my site is codingfriends.com\r\n\r\npyhon does OO\r\n\r\nseek to the start of the file\r\nhi there\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is how to open and read\/write to a file within Python. Most languages supply a basic file opening and read\/writing methods with the closing method, and Python is no different which makes going from one language to another very easy (you just need to learn the classes that are capable to doing the interesting &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2011\/05\/03\/read-write\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Read \/ Write<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[286],"tags":[344,345],"class_list":["post-1604","post","type-post","status-publish","format-standard","hentry","category-python","tag-read","tag-write"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1604"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1604\/revisions"}],"predecessor-version":[{"id":1605,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1604\/revisions\/1605"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}