{"id":678,"date":"2018-06-10T13:41:12","date_gmt":"2018-06-10T13:41:12","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=678"},"modified":"2019-08-12T08:18:49","modified_gmt":"2019-08-12T08:18:49","slug":"python-basics-of-file-handling","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=678","title":{"rendered":"Python : Basics of File Handling"},"content":{"rendered":"<p>In this tutorial we will be showing how to work with files stored in the host, such as reading the contents of the file, adding more lines, etc.<\/p>\n<p>Listed below are few of the syntax used in Python to work with files. We will be using them in the Python scripts posted later:<\/p>\n<ul>\n<li>Open file in read mode<br \/>\n<em><strong>theFile = open(&#8216;myFile.txt&#8217;, r)<\/strong><\/em><\/li>\n<li>Open file in append mode<br \/>\n<em><strong>theFile = open(&#8216;myFile.txt&#8217;, a)<\/strong><\/em><\/li>\n<li>Open file in write mode<br \/>\n<em><strong>theFile = open(&#8216;myFile.txt&#8217;, r+)<\/strong><\/em><\/li>\n<\/ul>\n<ul>\n<li>Read the entire file<br \/>\n<em><strong>theFile.read()<\/strong><\/em><\/li>\n<li>Read 4 bites of the file<br \/>\n<em><strong>theFile.read(4)<\/strong><\/em><\/li>\n<li>Read file line by line<br \/>\n<em><strong>theFile.readline<\/strong><\/em><\/li>\n<li>Read all lines in the file to a list<br \/>\n<em><strong>theFile.readlines<\/strong><\/em><\/li>\n<\/ul>\n<ul>\n<li>Write string to a file<br \/>\n<em><strong>theFile.write(&lt;string&gt;)<\/strong><\/em><\/li>\n<li>Closes the conenction to a file<br \/>\n<em><strong>theFile.close()<\/strong><\/em><\/li>\n<\/ul>\n<p>Let us work with the files now:<\/p>\n<ul>\n<li>Now let us create a simple text file with the following content:<br \/>\n<em><strong>[root@gw16-lap-devops webapp]# cat sample.txt<\/strong><\/em><br \/>\n<em>Line 1<\/em><br \/>\n<em>Line 2<\/em><br \/>\n<em>Line 3<\/em><br \/>\n<em>Line 4<\/em><br \/>\n<em>Line 5<\/em><br \/>\n<em>Line 6<\/em><br \/>\n<em>Line 7<\/em><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li>Let us write a Python script to read and print the contents of the file:<br \/>\n<strong><em>[root@gw16-lap-devops webapp]# cat readFile.py<\/em><\/strong><br \/>\n<em>#! \/usr\/bin\/python<\/em><br \/>\n<em>#<\/em><br \/>\n<em>myFile = open(&#8220;sample.txt&#8221;)<\/em><br \/>\n<em>print(myFile.read())<\/em><br \/>\n<em>myFile.close()<\/em><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li>Now if you run the file you will see the entire contents getting displayed.<br \/>\nSee if you can use the &#8220;myFile.read(8)&#8221; command in it and check the result.<\/li>\n<\/ul>\n<p>Let us write a Python script to write a line to the same file and then read and print the contents of the file:<\/p>\n<ul>\n<li><strong><em>[root@gw16-lap-devops webapp]# cat writeLines.py<\/em><\/strong><br \/>\n<em>#! \/usr\/bin\/python<\/em><br \/>\n<em>#<\/em><br \/>\n<em>theFile = open(&#8220;sample.txt&#8221;,&#8217;a&#8217;)<\/em><br \/>\n<em>theString = &#8216;\\nAdding a line&#8217;<\/em><br \/>\n<em>theFile.write(theString)<\/em><br \/>\n<em>theFile.close()<\/em><br \/>\n<em>#<\/em><br \/>\n<em>myFile = open(&#8220;sample.txt&#8221;)<\/em><br \/>\n<em>print(myFile.read())<\/em><br \/>\n<em>myFile.close()<\/em><\/li>\n<\/ul>\n<p>Let us write a Python script to write a line to the same file and then read and print the contents of the file:<\/p>\n<ul>\n<li><strong><em>[root@gw16-lap-devops webapp]# cat writeLines.py<\/em><\/strong><br \/>\n<em>#! \/usr\/bin\/python<\/em><br \/>\n<em>#<\/em><br \/>\n<em>theFileName = &#8216;sample.txt&#8217;<\/em><br \/>\n<em>myFile = open(theFileName)<\/em><br \/>\n<em>theFileList1 = myFile.readlines()<\/em><br \/>\n<em>myFile.close()<\/em><br \/>\n<em>totalLines=len(theFileList1)<\/em><br \/>\n<em>#<\/em><br \/>\n<em>#<\/em><br \/>\n<em>theFile = open(theFileName,&#8217;a&#8217;)<\/em><br \/>\n<em>lineNo = totalLines + 1<\/em><br \/>\n<em>for i in range(3):<\/em><br \/>\n<em> theString = &#8220;Line &#8221; + str(lineNo) + &#8220;\\n&#8221;<\/em><br \/>\n<em> theFile.write(theString)<\/em><br \/>\n<em> lineNo = lineNo + 1<\/em><br \/>\n<em>theFile.close()<\/em><br \/>\n<em>#<\/em><br \/>\n<em>#<\/em><br \/>\n<em>myFile1 = open(theFileName)<\/em><br \/>\n<em>print(myFile1.read())<\/em><br \/>\n<em>myFile1.close()<\/em><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this tutorial we will be showing how to work with files stored in the host, such as reading the contents of the file, adding <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=678\" title=\"Python : Basics of File Handling\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":518,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,22,27],"tags":[],"class_list":["post-678","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-programming","category-python"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=678"}],"version-history":[{"count":1,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/678\/revisions"}],"predecessor-version":[{"id":679,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/678\/revisions\/679"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/518"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}