{"id":4069,"date":"2020-07-20T16:37:28","date_gmt":"2020-07-20T11:07:28","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4069"},"modified":"2020-07-20T16:38:58","modified_gmt":"2020-07-20T11:08:58","slug":"python-file-handling","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-file-handling\/","title":{"rendered":"Python File Handling"},"content":{"rendered":"\n<p>Python File handling a method to store the output of the program to a file or take input from the file. File handling is a key concept in the programming world. File handling used in almost every kind of project. For example, you are creating an Inventory management system. In the inventory management system, you have data related to sales and purchases so you need to store that data somewhere. Using Python file handling you can store that data into the file. If you are trying to perform some analysis on the data then you must be provided with data in the form <a href=\"https:\/\/en.wikipedia.org\/wiki\/Comma-separated_values\" rel=\"nofollow noopener\" target=\"_blank\">comma separated file<\/a> or Microsoft Excel file. Using file handling you can read data from that file and also store output back into that file.<\/p>\n\n\n\n<p>While using file handling there are different types of modes available. Just have a look at them we will explain them in detail.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u201c<strong> r <\/strong>\u201c, for reading.<\/li><li>\u201c<strong> w <\/strong>\u201c, for writing.<\/li><li>\u201c<strong> a <\/strong>\u201c, for appending.<\/li><li>\u201c<strong> r+ <\/strong>\u201c, for both reading and writing<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Create file&nbsp;<\/h2>\n\n\n\n<p>Let\u2019s take a look at how to create a file using <a href=\"https:\/\/www.h2kinfosys.com\/blog\/how-should-i-start-learning-python\/\">Python programming <\/a>language.<\/p>\n\n\n\n<p>Take a look at the left side we have only hello.py file.&nbsp; <img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/hQFCQIVhWsVOIrzxilyffRx2JOIl6r2EW8wPVIPbRbuSk5QyP80QeQo8nGFSjf6472JUuFKcx8D13H9x4Um9jWCgJOpt4MDwbVXBlR7LFO5d2enpJEIXIjw15TNkWYh2KmjSdxkbEANMCjcPXQ\" width=\"624\" height=\"179\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>Now let\u2019s execute the code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f = open(\"data.txt\",\"w+\")<\/code><\/pre>\n\n\n\n<p>w+ stands for: Open a file for writing and reading.<\/p>\n\n\n\n<p>The file with name data.txt has been created.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/Izje4YUYMsSyNbsmjR4KHjPSWgQ_POqSXOW1zhYlTveNyzVvk0BLOLS1EeGDhOaayfrL79CEI3K5kqmnFlwtNROPdc7_m2gYYLXS3NVYdbhhQtJemQGtXciWS6dTmBskeTW8dba8B1xupwiLVA\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Open and write into file<\/h2>\n\n\n\n<p>We will be using the above-created file. Let\u2019s write some data into the file using code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f = open(\"data.txt\",\"w+\")\nfor i in range(5):\n\tf.write(\"line \" + str(i) + \"\\n\")<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/u3M-HuDhgH54oWjqfhZQAqqDQ-kQ-M2wmwhEEqQatAGwdP0V2YLqNhnSoyDnRLT6S83s2Mo_h3U_K-FK2zBt3y88ITs2iFaAYMy1lJj4s03S2lWJhUD9S_kPZLIA0yRMpsJl602xVfFTEMcMfA\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>The file looks like this<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/oLswTxyq1tYij-PHHdaYY-iB2mbAo9EvHo825VRJpgaOSYlz2IukqL2hmOJy-IxersFhOAj7Cmbql4iHhzbrg-lbB9YuwwtLnvk3dSozSgyk3wKz7oYPdWWNyEG8K9lwkDVZbSNJYpUdZE0C1g\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Append into file<\/h2>\n\n\n\n<p>\u201cWrite\u201d option erases all the previous data present in the file. For appending data into an existing file we need to use a mode of \u201ca\u201d.<\/p>\n\n\n\n<p>Let\u2019s take a look at the code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f = open(\"data.txt\",\"a\")\nfor i in range(2):\n\tf.write(\"appended lines \" + str(i) + \"\\n\")<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/bYxJUF5IDqL5RoH-Bg_3oQPIqzw5Tom7gyMpJgve0lBYIDpOBLYwMRsPNIGtSMQb2k2xptUaJ7gmE8d1hWSDqG7kM5Nxi8KjJiBdEmdcc2bc7X_PcwKn7da-EkYLlZviQLhNGPtDQhwDYXjV3Q\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>The data.txt file will look like this<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/U0z3Vnr-FOUWqkoEXRjM22z_f71UWQeEtGQKilZzjxcpRovS31o9_hWYplz5QfPLG66B9pVcbuhhEjYiuSG5mWWLVdOgaH_GUjBA6OACQCiCELJ-VmGL-rKMQV2qE8Hge4_8dY7h0bpy3HejMg\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Read data from file<\/h2>\n\n\n\n<p>Let\u2019s take a look at code to read data from the data.txt file.<\/p>\n\n\n\n<p>f.read function output all the data present in the file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f = open(\"data.txt\",\"r\")\nprint(f.read())<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/EAtMymf26JugPE9rQ8Uk_5SlIOOqeqmW-OxUwd0u1lStz5Sn4jcYsvD-x1WHDUgYpgOd0kDJXCMilNZKKaB4quvW_Hy08L7459-AxSn-N10JlRJ7hGEgmBnAof0jgX2uAnewbpLrSF9NTOpZXg\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>If we need to read the data line by line we use readline() function.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/DCW-RFXURz3hWpk6wmUmrB3gCN1Yd3PFaCuZ3ms3jWbin3i-ll6gp8Ce9rFkoKP1EkhRAwk7VhnY6cI2vQAdA81z3tPfGfSjHeFbpwJj9La7TSZlJBKJ2VO5Hy1sWeSZSKzJMfHSzu008bGZtw\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>For reading first two lines.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/PaQUdt8qzGb3dOvgZvd7eo58ibmhN3oS5ApqfZYPy1m9UU5Afyr8h5OAHMuGcqojam9GMIQQqmv4nw4y-yV2RJ7QhvCZyeBfxns9BzdvR_5BQD7ngbl8BaEUEfVXnRD4tzzGYkF7PwJrYL1wVg\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python File handling a method to store the output of the program to a file or take input from the file. File handling is a key concept in the programming world. File handling used in almost every kind of project. For example, you are creating an Inventory management system. In the inventory management system, you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4072,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[1088,1120,1121,1119,1122,1123],"class_list":["post-4069","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-append","tag-create","tag-open","tag-python-file-handling","tag-read","tag-write"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4069"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4069\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/4072"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}