{"id":39052,"date":"2016-11-23T08:55:54","date_gmt":"2016-11-23T14:55:54","guid":{"rendered":"http:\/\/a00344371.wordpress.com\/?p=325"},"modified":"2016-11-23T08:55:54","modified_gmt":"2016-11-23T14:55:54","slug":"reading-and-writing-of-text-files-6","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/reading-and-writing-of-text-files-6\/","title":{"rendered":"Reading and writing of text files"},"content":{"rendered":"<p class=\"syndicated-attribution\"><em> --Originally published at <a href=\"https:\/\/a00344371.wordpress.com\/2016\/11\/23\/reading-and-writing-of-text-files\/\"><a href=\"https:\/\/a00344371.wordpress.com\">Programming Fundaments<\/a><\/a><\/em><br \/><\/p>\n<p>In Python, you don&#8217;t need to import any library to read and write files., you just eed the flie to open, and for doing so, you will have to use the &#8220;open()&#8221; command:<\/p>\n<pre>file_object = open(filename, mode) #where file_object is the variable to put the\nfile object.<\/pre>\n<p>There are two types of files: text and binary;\u00a0A text file is often structured as a sequence of lines and a line is a sequence of characters. And a\u00a0binary file is basically any file that is not a text file. Binary files can only be processed by application that know about the file&#8217;s structure.<\/p>\n<p>and you have to define a mode, which the are some type of:<\/p>\n<ul>\n<li>&#8216;r&#8217; when the file will only be read<\/li>\n<li>&#8216;w&#8217; for only writing (an existing file with the same name will be erased)<\/li>\n<li>&#8216;a&#8217; opens the file for appending; any data written to the file is automatically added to the end.<\/li>\n<li>&#8216;r+&#8217; opens the file for both reading and writing.<\/li>\n<\/ul>\n<p>At the end it will create something like this:<\/p>\n<pre class=\"brush:&quot;python&quot;\">&gt;&gt;&gt; f = open('workfile', 'w')\n&gt;&gt;&gt; print f<\/pre>\n<p>To create an text file just follow the example:<\/p>\n<pre class=\"brush:&quot;python&quot;\">file = open(\"newfile.txt\", \"w\")\n\nfile.write(\"hello world in the new file\n\")\n\nfile.write(\"and another line\n\")\n\nfile.close()<\/pre>\n<p>Resulting in:<\/p>\n<pre class=\"brush:&quot;python&quot;\">$ cat newfile.txt \nhello world in the new file\nand another line<\/pre>\n<p>Examples and More at:\u00a0http:\/\/www.pythonforbeginners.com\/files\/reading-and-writing-files-in-python<\/p><br \/>  <a rel=\"nofollow\" href=\"http:\/\/feeds.wordpress.com\/1.0\/gocomments\/a00344371.wordpress.com\/325\/\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/a00344371.wordpress.com\/325\/\" \/><\/a> <img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=a00344371.wordpress.com&#038;blog=115285426&%23038;post=325&%23038;subd=a00344371&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/>","protected":false},"excerpt":{"rendered":"<p>&#8211;Originally published at Programming Fundaments In Python, you don&#8217;t need to import any library to read and write files., you just eed the flie to open, and for doing so, you will have to use the &#8220;open()&#8221; command: file_object = open(filename, mode) #where file_object is the variable to put the file object. There are two &hellip; <a href=\"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/reading-and-writing-of-text-files-6\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Reading and writing of text files<\/span><\/a><\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[37,24,33],"class_list":["post-39052","post","type-post","status-publish","format-standard","hentry","category-blogs","category-syndicated","tag-programming","tag-python","tag-tc101"],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/39052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/comments?post=39052"}],"version-history":[{"count":5,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/39052\/revisions"}],"predecessor-version":[{"id":43091,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/39052\/revisions\/43091"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media?parent=39052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/categories?post=39052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/tags?post=39052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}