{"id":3592,"date":"2017-05-18T22:52:52","date_gmt":"2017-05-18T18:52:52","guid":{"rendered":"https:\/\/nayarweb.com\/blog\/?p=3592"},"modified":"2017-05-18T22:56:41","modified_gmt":"2017-05-18T18:56:41","slug":"reading-files-line-by-line-in-python","status":"publish","type":"post","link":"https:\/\/nayarweb.com\/blog\/2017\/reading-files-line-by-line-in-python\/","title":{"rendered":"Reading Files Line by Line in Python"},"content":{"rendered":"<p>You write a script in python which has to read a file, loop through each line to do some work. Seems easy right?<\/p>\n<pre>f = open('loulou.txt', 'r')\r\nlines = f.readlines()\r\nfor line in lines:\r\n\tprint line\r\n<\/pre>\n<p>Problem is when your file becomes big, let&#8217;s say 1500MB, the code will still run on your development laptop but it will fail on a server on cloud with 1GB of RAM. You&#8217;ll have an Out Of Memory error which can lead to some very important process being killed if OOM priorities are not mastered.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3595\" src=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/05\/Screen-Shot-2017-05-18-at-22.06.44.png\" alt=\"\" width=\"723\" height=\"299\" srcset=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/05\/Screen-Shot-2017-05-18-at-22.06.44.png 723w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/05\/Screen-Shot-2017-05-18-at-22.06.44-300x124.png 300w\" sizes=\"auto, (max-width: 723px) 100vw, 723px\" \/><\/p>\n<p>You can use the <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/nayarweb.com\/collectiva\">Collectiva service by NAYARWEB.COM<\/a><\/span> to get alerted near-realtime whenever Out of Memory occurs on one of your servers.<\/p>\n<p>To fix the code above, you can read the file line by line from the disk:<\/p>\n<pre>f = open('loulou.txt', 'r')\r\nfor line in f:\r\n        print line<\/pre>\n<p>Is this always better? No. The current version will run slightly slower than the first code depending on your storage backend.<\/p>\n<p>It&#8217;s amazing how little differences like these can have a huge impact on production servers.<\/p>\n<p>Happy Monitoring to all System Admins out there \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You write a script in python which has to read a file, loop through each line to do some work. Seems easy right? f = open(&#8216;loulou.txt&#8217;, &#8216;r&#8217;) lines = f.readlines() for line in lines: print line Problem is when your file becomes big, let&#8217;s say 1500MB, the code will still run on your development laptop &hellip; <a href=\"https:\/\/nayarweb.com\/blog\/2017\/reading-files-line-by-line-in-python\/\" class=\"continue-reading\">Continue reading <span class=\"screen-reader-text\">Reading Files Line by Line in Python<\/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":[210],"tags":[],"class_list":["post-3592","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/3592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/comments?post=3592"}],"version-history":[{"count":4,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/3592\/revisions"}],"predecessor-version":[{"id":3598,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/3592\/revisions\/3598"}],"wp:attachment":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/media?parent=3592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/categories?post=3592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/tags?post=3592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}