{"id":38006,"date":"2016-10-14T17:34:55","date_gmt":"2016-10-14T22:34:55","guid":{"rendered":"http:\/\/danigguemez.wordpress.com\/?p=458"},"modified":"2016-10-14T17:34:55","modified_gmt":"2016-10-14T22:34:55","slug":"files-in-python","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/files-in-python\/","title":{"rendered":"Files in Python"},"content":{"rendered":"<p class=\"syndicated-attribution\"><em> --Originally published at <a href=\"https:\/\/danigguemez.wordpress.com\/2016\/10\/14\/files-in-python\/\"><a href=\"https:\/\/danigguemez.wordpress.com\">Fundamentos de Programaci\u00f3n<\/a><\/a><\/em><br \/><\/p>\n<p>In the blog about counting words, I used the funtion open() to coun the words in that file but I haven&#8217;t made a post explaining how to do that, so here it is:<\/p>\n<p>In Python, you don&#8217;t need to import any library to read and write files.<\/p>\n<p>The first step is to get a file object.<\/p>\n<p>The way to do this is to use the open function.<\/p>\n<p>A file is usually categorized as either text or binary.<\/p>\n<p><img class=\"alignnone size-full wp-image-461\" src=\"https:\/\/danigguemez.files.wordpress.com\/2016\/10\/vmveq3w_700wa_0.gif?w=676\" alt=\"vmveq3w_700wa_0\"   \/><\/p>\n<p>A text file is often structured as a sequence of lines and a line is a sequence<\/p>\n<p>of characters.<\/p>\n<p>The line is terminated by a EOL (End Of Line) character.<\/p>\n<p>The backslash character indicates that the next character will be treated as a<\/p>\n<p>newline.<\/p>\n<p>A binary file is basically any file that is not a text file. Binary files can<\/p>\n<p>only be processed by application that know about the file&#8217;s structure.<\/p>\n<p>To open a file for writing use the built-i open() function. open() returns a<\/p>\n<p>file object, and is most commonly used with two arguments.<\/p>\n<p>&nbsp;<\/p>\n<p>The syntax is:<\/p>\n<p>file_object = open(filename, mode) where file_object is the variable to put the<\/p>\n<p>file object.<\/p>\n<p>&nbsp;<\/p>\n<p>The second argument describes the way in which the file will be used<\/p>\n<p>The mode argument is optional; &#8216;r&#8217; will be assumed if it\u2019s omitted.<\/p>\n<p><img class=\"alignnone size-full wp-image-464\" src=\"https:\/\/danigguemez.files.wordpress.com\/2016\/10\/lmzjvmm_700w_0.jpg?w=676\" alt=\"lmzjvmm_700w_0\"   \/><\/p>\n<p>The modes can be:<\/p>\n<p>&#8216;r&#8217; when the file will only be read<\/p>\n<p>&#8216;w&#8217; for only writing (an existing file with the same name will be erased)<\/p>\n<p>&#8216;a&#8217; opens the file for appending; any data written to the file is automatically<\/p>\n<p>added to the end.<\/p>\n<p>&#8216;r+&#8217; opens the file for both reading and writing.<\/p>\n<p><img class=\"alignnone size-full wp-image-224\" src=\"https:\/\/danigguemez.files.wordpress.com\/2016\/08\/ymkd7rq_700wa_0.gif?w=676\" alt=\"ymkd7rq_700wa_0\"   \/><\/p>\n<p><strong>Create a text file<\/strong><\/p>\n<p>Let&#8217;s first create a new text file. You can name it anything you like,<\/p>\n<p>in this example we will name it &#8220;newfile.txt&#8221;.<\/p>\n<p>file = open(&#8220;newfile.txt&#8221;, <div class=\"post-limited-image\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/danigguemez.wordpress.com\/458\/\" \/><\/div>\n<div class=\"post-limited-image\"><img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=danigguemez.wordpress.com&#038;blog=115238226&%23038;post=458&%23038;subd=danigguemez&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/><\/div><!--more--><\/p>\n<p>file.write(&#8220;hello world in the new file<\/p>\n<p>&#8220;)<\/p>\n<p>file.write(&#8220;and another line<\/p>\n<p>&#8220;)<\/p>\n<p>file.close()<\/p>\n<p><strong>How to read a text file<\/strong><\/p>\n<p>To read a file, we can use different methods.<\/p>\n<p>file.read( )<\/p>\n<p>If you want to return a string containing all characters in the file, you can<\/p>\n<p>use file.read().<\/p>\n<p>file = open(&#8216;newfile.txt&#8217;, &#8216;r&#8217;)<\/p>\n<p>print file.read()<\/p>\n<p>Output:<\/p>\n<p>hello world in the new file<\/p>\n<p>and another line<\/p>\n<p>A video that explains it:<\/p>\n<div class=\"jetpack-video-wrapper\"><iframe class='youtube-player' type='text\/html' width='676' height='411' src='https:\/\/www.youtube.com\/embed\/Uh2ebFW8OYM?version=3&#038;rel=1&%23038;fs=1&%23038;autohide=2&%23038;showsearch=0&%23038;showinfo=1&%23038;iv_load_policy=1&%23038;wmode=transparent' allowfullscreen='true' style='border:0;'><\/iframe><\/div>\n<p>Thanks for reading.<\/p>\n<p>Follow me on twitter: <a href='https:\/\/danigguemez.wordpress.com\/mentions\/danigguemez\/' class='mention'><span class='mentions-prefix'>@<\/span>danigguemez<\/a><\/p>\n<p>#TC101 #python #files<\/p>\n<p>Sources:<a href=\"http:\/\/www.pythonforbeginners.com\/files\/reading-and-writing-files-in-python\" rel=\"nofollow\">http:\/\/www.pythonforbeginners.com\/files\/reading-and-writing-files-in-python<\/a><\/p>\n<p><a href=\"https:\/\/www.sololearn.com\/User\/Login\/?ReturnUrl=%2FPlay%2FPython\" rel=\"nofollow\">https:\/\/www.sololearn.com\/User\/Login\/?ReturnUrl=%2fPlay%2fPython<\/a><\/p>\n<p>&nbsp;<\/p><br \/>  <a rel=\"nofollow\" href=\"http:\/\/feeds.wordpress.com\/1.0\/gocomments\/danigguemez.wordpress.com\/458\/\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/danigguemez.wordpress.com\/458\/\" \/><\/a> <img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=danigguemez.wordpress.com&#038;blog=115238226&%23038;post=458&%23038;subd=danigguemez&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/>","protected":false},"excerpt":{"rendered":"<p>&#8211;Originally published at Fundamentos de Programaci\u00f3n In the blog about counting words, I used the funtion open() to coun the words in that file but I haven&#8217;t made a post explaining how to do that, so here it is: In Python, you don&#8217;t need to import any library to read and write files. The first &hellip; <a href=\"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/files-in-python\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Files in Python<\/span><\/a><\/p>\n","protected":false},"author":8,"featured_media":461,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[28],"class_list":["post-38006","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogs","category-syndicated","tag-uncategorized"],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38006","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\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/comments?post=38006"}],"version-history":[{"count":4,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38006\/revisions"}],"predecessor-version":[{"id":40700,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38006\/revisions\/40700"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media?parent=38006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/categories?post=38006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/tags?post=38006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}