{"id":2379,"date":"2020-01-08T17:29:39","date_gmt":"2020-01-08T17:29:39","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2379"},"modified":"2023-02-16T19:57:19","modified_gmt":"2023-02-16T19:57:19","slug":"python-file-handling","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-file-handling","title":{"rendered":"Python File Handling"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is Python File Handling?<\/h2>\n\n\n\n<p><code><strong>File handling<\/strong><\/code> is basically the management of the files on a file system. Every operating system has its own way to store files.<\/p>\n\n\n\n<p>Python File handling is useful to work with files in our programs. We don&#8217;t have to worry about the underlying operating system and its file system rules and operations. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"511\" height=\"761\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/File-operations.png\" alt=\"File Operations\" class=\"wp-image-2386\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/File-operations.png 511w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/File-operations-201x300.png 201w\" sizes=\"auto, (max-width: 511px) 100vw, 511px\" \/><figcaption>File Operations<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. open() function<\/h3>\n\n\n\n<p>The open() function is used to open a file in a particular mode.<\/p>\n\n\n\n<p>It basically creates a file object which can be used for further manipulations.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> open(file_name, mode) <\/code><\/pre>\n\n\n\n<p><strong>Different modes for opening a file:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>r<\/strong>: Read<\/li><li><strong>w<\/strong>: Write<\/li><li><strong>a<\/strong>: Append<\/li><li><strong>r<\/strong>+: Read and Write <\/li><\/ul>\n\n\n\n<p>Initially, we need to create a file and place it in the same directory as of the script.<\/p>\n\n\n\n<p><strong><em>Demo.txt<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to the programming world!<\/code><\/pre>\n\n\n\n<p><strong><em>Execute_file.py<\/em><\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndemo_file = open(&#039;Demo.txt&#039;, &#039;r&#039;)\n# This statement will print every line in the file\nfor x in demo_file:\n    print (x)\n\n# close the file, very important\ndemo_file.close()\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to the programming world!<\/code><\/pre>\n\n\n\n<p>Here, the Execute_file.py script opens the Demo.txt file and prints the entire content line-by-line.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. read() function<\/h3>\n\n\n\n<p>The read() function is used to read the contents of the file. To achieve the same, we need to open a file in the read mode.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndemo_file = open(&quot;Demo.txt&quot;, &quot;r&quot;)\nprint(demo_file.read())\ndemo_file.close()\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to the programming world!<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. write() function<\/h3>\n\n\n\n<p>The write() function is used to write to a file and make changes to it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndemo_file = open(&#039;Demo.txt&#039;,&#039;w&#039;)\ndemo_file.write(&quot;Hello Everyone!.\\n&quot;)\ndemo_file.write(&quot;Engineering Discipline.&quot;)\ndemo_file.close()\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong> When we open the Demo.txt file, we can see the changes reflected here.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Everyone!.\nEngineering Discipline.<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. append() function<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndemo_file = open(&#039;Demo.txt&#039;,&#039;a&#039;)\n\ndemo_file.write(&quot;\\nStatement added to the end of the file..&quot;)\ndemo_file.close()\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Everyone!.\nEngineering Discipline.\nStatement added to the end of the file..<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. split() function<\/h3>\n\n\n\n<p>The split() function is used to split lines within a file. It splits up as soon as it encounters space in the script.<\/p>\n\n\n\n<p><strong><em>Demo.txt<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Everyone!.\nEngineering Discipline.\nStatement added to the end of the file..<\/code><\/pre>\n\n\n\n<p><strong><em>Execute_file.py<\/em><\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nwith open(&quot;Demo.txt&quot;, &quot;r&quot;) as demo_file:\n    demo_data = demo_file.readlines()\n    for line in demo_data:\n        result = line.split()\n        print(result)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>['Hello', 'Everyone!.']\n['Engineering', 'Discipline.']\n['Statement', 'added', 'to', 'the', 'end', 'of', 'the', 'file..']<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. close() function<\/h3>\n\n\n\n<p>The <code><strong>close()<\/strong><\/code> function is used to close a particular file post manipulations on it.<\/p>\n\n\n\n<p>After writing to a file, if we do not call the close() method, all the data written to the file will not be saved in it.<\/p>\n\n\n\n<p>It&#8217;s always a good idea to close the file after we are done with it to release the resources.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file-name.close()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. rename() function<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/www.askpython.com\/python-modules\/python-os-module-10-must-know-functions\" class=\"rank-math-link\">os module <\/a>provides the <code><strong>rename()<\/strong><\/code> method to change the name of the particular file.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>os.rename(current_name,new_name)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. remove() method<\/h3>\n\n\n\n<p>The os module provides the <code><strong>remove()<\/strong><\/code> method to delete the file given as input.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nos.remove(&#039;Demo.txt&#039;)\n<\/pre><\/div>\n\n\n<p><strong><em>Before executing the remove() method..<\/em><\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"478\" height=\"194\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Delete-a-file.png\" alt=\"Delete A File\" class=\"wp-image-2406\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Delete-a-file.png 478w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Delete-a-file-300x122.png 300w\" sizes=\"auto, (max-width: 478px) 100vw, 478px\" \/><figcaption>Before remove()<\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>Output:<\/strong> <strong><em>After executing the remove() method<\/em><\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"157\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/After-remove.png\" alt=\"After Remove\" class=\"wp-image-2388\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/After-remove.png 470w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/After-remove-300x100.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><figcaption>After remove()<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the File operations in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python File Handling<\/li><li><a class=\"rank-math-link rank-math-link\" href=\"https:\/\/docs.python.org\/3\/tutorial\/inputoutput.html\" target=\"_blank\" rel=\"noopener\">File operations documentation<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>What is Python File Handling? File handling is basically the management of the files on a file system. Every operating system has its own way to store files. Python File handling is useful to work with files in our programs. We don&#8217;t have to worry about the underlying operating system and its file system rules [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2379","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2379"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2379\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}