{"id":38064,"date":"2016-10-20T12:34:42","date_gmt":"2016-10-20T17:34:42","guid":{"rendered":"https:\/\/kenscourses.com\/tc101fall2016\/?guid=cee96562f572d4b4910a413c04e268aa"},"modified":"2016-10-20T12:34:42","modified_gmt":"2016-10-20T17:34:42","slug":"do-not-read-this","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/do-not-read-this\/","title":{"rendered":"DO NOT READ THIS!!"},"content":{"rendered":"<p class=\"syndicated-attribution\"><em> --Originally published at <a href=\"http:\/\/emnl.me\/do-not-read-this\/\"><a href=\"http:\/\/emnl.me\/\">Coding The Future<\/a><\/a><\/em><br \/><\/p>\n<p><img src=\"http:\/\/emnl.me\/content\/images\/2016\/10\/donotread.png\" alt=\"\" title=\"\">Original image by <a href=\"https:\/\/unsplash.com\/search\/text?photo=ywqa9IZB-dU\">Romain Vignes<\/a>.<\/p>\n\n<p>Hello! Ok, I must begging by saying that reading this article will not kill you nor hurt you in any way. In fact, it will be beneficial! So why the title, you may ask? Just to catch your attention! ?<\/p>\n\n<p>Today's topic is <strong>reading and writing<\/strong> text files. This is one of the most important aspects in programming, because if you think about it, 99% of things online are text-based. Think about it... although Google has the option to search through voice and image, 99.9% of the times you probably just type what you need to find and hit enter. See? This is why programs must be able to interpret text because we live in a text-based world.<\/p>\n\n<p>I want to get things clear though... When I say text files, I am not referring to MS Word documents \u2013those are processed word docs\u2013 instead of plain .txt files. Let's get going.<\/p>\n\n<h4 id=\"openingtextfiles\">Opening text files<\/h4>\n\n<p>Let's begging by looking at how to open a text file, which is the first step to reading or writing on the file.<\/p>\n\n<p>In order to do this, we will need to declare a variable where a text file object will be stored. To do so, use the <em>open( )<\/em> method and pass the filename as a parameter.<\/p>\n\n<p><code>textFile = open(loremIpsum.txt)<\/code><\/p>\n\n<h4 id=\"readingtextfiles\">Reading text files<\/h4>\n\n<p>Once we have the text stored in <em>textFile<\/em>, our sample variable, we can read it to process its contents. To do so, we need to use the <em>.read()<\/em> function. <\/p>\n\n<p>In the following sample code, I read the text file I previously imported and print it:<\/p>\n\n<p><code>print(textFile.read())<\/code><\/p>\n\n<p>As you can see, since the text file is already stored in the <em>textFile<\/em> variable, I use the function within the file object.<\/p>\n\n<h4 id=\"processingfiles\">Processing files<\/h4>\n\n<p>By processing, <!--more--> mean not just reading the plain text, but doing stuff with it. For example, we can read line by line using the <em>.readline()<\/em> function, or even read word by word by reading the text and storing it in a variable, and then splitting it using the <em>.split()<\/em> function. <\/p>\n\n<p>Take a look at this sample code, which splits the text into individual words and prints them:<\/p>\n\n<p><code>textFile = open('loremipsum.txt')\ntextFile = textFile.read()<\/code> <br>\n<code>for word in textFile.split():\n    print(word)<\/code><\/p>\n\n<p>If you wanted to search for a word in specific you could use a for loop and compare every word to the desired one.<\/p>\n\n<p>Anyways, the possibilities are endless.<\/p>\n\n<h4 id=\"writingtofiles\">Writing to Files<\/h4>\n\n<p>When we open a file, it is set to reading mode by default, but if we include an additional parameter, a simple 'w', we change to write and wipe mode. Take a look:<\/p>\n\n<p><code>textFile = open('loremipsum.txt', 'w')<\/code><\/p>\n\n<p>Once you have opened the file this way, using the <em>.write()<\/em> function, you can write text to the file. WACTCH OUT THOUGH! This method completely overwrites the previous text on the file.<\/p>\n\n<p>If we wanted to add text, we would need to open the file and pass 'a' as a parameter instead of 'w'.<\/p>\n\n<p><code>textFile = open('loremipsum.txt', 'a')\ntextFile.write(\"I love Latin texts!\")<\/code><\/p>\n\n<h4 id=\"closingthefile\">Closing the file<\/h4>\n\n<p>Even though many people skip this last step, this last thing is a good practise. After you are done reading your file, it is important to <em>.close()<\/em> your file, because otherwise your changes may not be reflected.<\/p>\n\n<p>Before leaving, I invite you to check out <a href=\"http:\/\/opentechschool.github.io\/python-data-intro\/core\/text-files.html\">OpenTechSchool<\/a> which really helped me refresh my memory on this topic.<\/p>\n\n<p>That's it for now... Expect another blog post tonight, I'm really in a writing mode!<\/p>\n\n<p><a href=\"http:\/\/twitter.com\/emamex98\">@emamex98<\/a><\/p>\n\n<div id=\"disqus_thread\"><\/div>  \n\n<script type=\"text\/javascript\">  \n    var disqus_shortname = 'emnl-me';\n    var disqus_identifier = 'read-write';\n\n    (function() {\n        var dsq = document.createElement('script'); dsq.type = 'text\/javascript'; dsq.async = true;\n        dsq.src = '\/\/' + disqus_shortname + '.disqus.com\/embed.js';\n        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);\n    })();\n<\/script>  \n\n<noscript>Please enable JavaScript to view the <a href=\"http:\/\/disqus.com\/?ref_noscript\">comments powered by Disqus.<\/a><\/noscript>  \n\n<p><a href=\"http:\/\/disqus.com\/\" class=\"dsq-brlink\">comments powered by <span class=\"logo-disqus\">Disqus<\/span><\/a> <\/p>\n\n<p>Additional sources: <br>\n<a href=\"http:\/\/stackoverflow.com\/questions\/7745260\/iterate-through-words-of-a-file-in-python\">StackOverflow 1<\/a><br>\n<a href=\"http:\/\/stackoverflow.com\/questions\/22441803\/python-write-to-file-without-overwriting-current-txt\">StackOwerflow 2<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>&#8211;Originally published at Coding The Future Original image by Romain Vignes. Hello! Ok, I must begging by saying that reading this article will not kill you nor hurt you in any way. In fact, it will be beneficial! So why the title, you may ask? Just to catch your attention! ? Today&#8217;s topic is reading &hellip; <a href=\"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/do-not-read-this\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">DO NOT READ THIS!!<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":38065,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[48,49,289,290],"class_list":["post-38064","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogs","category-syndicated","tag-input","tag-output","tag-reading","tag-writing"],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38064","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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/comments?post=38064"}],"version-history":[{"count":1,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38064\/revisions"}],"predecessor-version":[{"id":38066,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38064\/revisions\/38066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media\/38065"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media?parent=38064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/categories?post=38064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/tags?post=38064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}