

{"id":8404,"date":"2018-02-17T13:12:03","date_gmt":"2018-02-17T07:42:03","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=8404"},"modified":"2026-04-20T15:37:27","modified_gmt":"2026-04-20T10:07:27","slug":"file-handling-in-python","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/","title":{"rendered":"File Handling In Python &#8211; Python Read And Write File"},"content":{"rendered":"<p>Yesterday, we told you about File I\/O in Python.<strong>\u00a0<\/strong>Today, we will see File Handling in Python, in which we study: Python Read File, Python Write File, Python Open File, and Python Close File.<\/p>\n<p>Along with this, we will learn File Methods in Python with their syntax and examples.<\/p>\n<p>So, let&#8217;s start exploring File Handling in Python.<\/p>\n<h3>What is Python Read and Write File?<\/h3>\n<p><strong>Uses of file handling in Python:<\/strong><\/p>\n<ul>\n<li><strong>Saving data:<\/strong> Settings, app data, or results can be stored to use later.<\/li>\n<li><strong>Handling different types of files:<\/strong> Both text files and binary files, like images and audio, can be handled.<\/li>\n<li><strong>Data analysis:<\/strong> To make reports, read big data files like CSV or JSON to find useful information.<\/li>\n<\/ul>\n<p>In our previous article, we saw Python open, close, read, and write to a file. Let\u2019s take examples for each of Python&#8217;s Read and write files.<\/p>\n<h4>1. Open File in Python<\/h4>\n<p>In Python, to open a file, we use the open() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt; todo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt','r+')<\/pre>\n<p>We may choose from many opening modes for a file:<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"139\"><strong>Mode<\/strong><\/td>\n<td width=\"484\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"139\">r<\/td>\n<td width=\"484\">To read a file (default)<\/td>\n<\/tr>\n<tr>\n<td width=\"139\">w<\/td>\n<td width=\"484\">To write a file, creates a new file if it doesn\u2019t exist, and truncates if it does<\/td>\n<\/tr>\n<tr>\n<td width=\"139\">x<\/td>\n<td width=\"484\">Exclusive creation; fails if file already exists<\/td>\n<\/tr>\n<tr>\n<td width=\"139\">a<\/td>\n<td width=\"484\">To append at the end of the file; create if it doesn\u2019t exist<\/td>\n<\/tr>\n<tr>\n<td width=\"139\">t<\/td>\n<td width=\"484\">Text mode (default)<\/td>\n<\/tr>\n<tr>\n<td width=\"139\">b<\/td>\n<td width=\"484\">Binary mode<\/td>\n<\/tr>\n<tr>\n<td width=\"139\">+<\/td>\n<td width=\"484\">To open a file for updating (reading or writing)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2. Close File in Python<\/h4>\n<p>To close a file in Python, we use the close() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.close()<\/pre>\n<p>A safer practice is to put it inside a try..finally block.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; try:\r\n\ttodo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt','r+')\r\n\tprint(1\/0)\r\nfinally:\r\n\ttodo.close()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#127&gt;&#8221;, line 3, in &lt;module&gt;<\/p>\n<p>print(1\/0)<\/p>\n<p>ZeroDivisionError: division by zero<\/p>\n<\/div>\n<p>We can also use the with-statement so that the file automatically closes as the code under it finishes executing.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; with open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt','r+') as todo:\r\n\ttodo.read()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Get groceries\\nOrganize room\\nPrint labels\\nWrite article\\nStudy for exam\\nLearn to cook&#8217;<\/div>\n<h4>3. Read File in Python<\/h4>\n<p>To read from a file in Python, we call the read() method on the file object.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; type(todo)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;_io.TextIOWrapper&#8217;&gt;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt','r')\r\n&gt;&gt;&gt; todo.read(3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Get&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.read()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216; groceries\\nOrganize room\\nPrint labels\\nWrite article\\nStudy for exam\\nLearn to cook&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.read()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8221;<\/div>\n<p>When we provide an argument to read(), it reads that many characters. After that, calling read() reads from where the cursor is right now.<\/p>\n<p>To find the cursor\u2019s position, and to reposition it, we use the seek() and tell() methods, respectively.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.tell()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">88<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.seek(8)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">8<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.read()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;eries\\nOrganize room\\nPrint labels\\nWrite article\\nStudy for exam\\nLearn to cook&#8217;<\/div>\n<h4>4. Python readline() and readlines()<\/h4>\n<p>The method readline() reads one line at a time.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.seek(0)<\/pre>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readline()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Get groceries\\n&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readline()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Organize room\\n&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readline()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Print labels\\n&#8217;<\/div>\n<p>Now, the readlines() method prints the rest of the file.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readlines()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;Write article\\n&#8217;, &#8216;Study for exam\\n&#8217;, &#8216;Learn to cook&#8217;]<\/div>\n<p>Otherwise, we can also iterate on a file using a for loop.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.seek(0)<\/pre>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for line in todo:\r\n\tprint(line,end='')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Get groceries<br \/>\nOrganize room<br \/>\nPrint labels<br \/>\nWrite article<br \/>\nStudy for exam<br \/>\nLearn to cook<\/div>\n<h4>5. Write to a File in Python<\/h4>\n<p>Writing is just as friendly. Open with &#8220;w&#8221; to start fresh or &#8220;a&#8221; to append. Call f.write(&#8220;New record\\n&#8221;) and Python flushes data to disk. Add print(&#8220;log&#8221;, file=f) for an instantly clear log file.<\/p>\n<p>Refer to the table above to find out more about these.<\/p>\n<p>Let\u2019s first close the file.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.close()\r\n&gt;&gt;&gt; todo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt','a')\r\n&gt;&gt;&gt; todo.write('Write to the end of the file')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">28<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.close()\r\n&gt;&gt;&gt; with open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt','r') as todo:\r\n\ttodo.read()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Get groceries\\nOrganize room\\nPrint labels\\nWrite article\\nStudy for exam\\nLearn to cookWrite to the end of the file&#8217;<\/div>\n<p>Any doubts yet in File handling in Python? Please comment.<\/p>\n<h3>Python Read and Write File &#8211; Python File Methods<\/h3>\n<p>Let\u2019s now look at some\u00a0methods to deal with Python Read and Write File.<\/p>\n<div id=\"attachment_8510\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-File-Methods-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-8510\" class=\"wp-image-8510 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-File-Methods-01.jpg\" alt=\"Python Read and Write File - Python File Methods\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-File-Methods-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-File-Methods-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-File-Methods-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-File-Methods-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-File-Methods-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-8510\" class=\"wp-caption-text\">Python Read and Write File &#8211; Python File Methods<\/p><\/div>\n<h4>1. close() in Python<\/h4>\n<p>With close(), we close a file to free up the resources held by it.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt;todo.close()<\/pre>\n<p>You must always close a file after you\u2019re done working with it. Check section 2b above.<\/p>\n<h4>2. detach() in Python<\/h4>\n<p>This detaches the underlying binary buffer from TextIOBase and returns it.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.detach()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;_io.BufferedRandom name=&#8217;C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt&#8217;&gt;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.read()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#171&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>todo.read()<\/p>\n<p>ValueError: underlying buffer has been detached<\/p>\n<\/div>\n<p>After detaching from the buffer, when we try to read the file, it raises a ValueError.<\/p>\n<h4>3. fileno() in Python<\/h4>\n<p>fileno() returns a file descriptor of the file. This is an integer number.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt')\r\n&gt;&gt;&gt; todo.fileno()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.fileno()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; myfile=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\Today.txt')\r\n&gt;&gt;&gt; myfile.fileno()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; gifts=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\gifts.jpg')\r\n&gt;&gt;&gt; gifts.fileno()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">5<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; one=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\1.txt')\r\n&gt;&gt;&gt; one.fileno()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">6<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; one.read()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8221;<\/div>\n<h4>4. flush() in Python<\/h4>\n<p>flush() writes the specified content from the program buffer to the operating system buffer in event of a power cut.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.flush()\r\n&gt;&gt;&gt;<\/pre>\n<h4>5. isatty() in Python<\/h4>\n<p>This method returns True if the file is connected to a tty-like device.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.isatty()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>6. read(n) in Python<\/h4>\n<p>This lets us read n characters from a file.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt')\r\n&gt;&gt;&gt; todo.read(5)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Get g&#8217;<\/div>\n<h4>7. readable() in Python<\/h4>\n<p>This returns True if the object is readable.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readable()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.close()\r\n&gt;&gt;&gt; todo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt','a')\r\n&gt;&gt;&gt; todo.readable()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.close()<\/pre>\n<h4>8. readline(n=-1) in Python<\/h4>\n<p>readline() reads the next line.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt')\r\n&gt;&gt;&gt; todo.readline()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Get groceries\\n&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readline(5)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Organ&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readline(2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;iz&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readline()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;e room\\n&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readline(5)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Print&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readline(-1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216; labels\\n&#8217;<\/div>\n<h4>9. readlines() in Python<\/h4>\n<p>This one reads the rest of the lines.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.seek(0)<\/pre>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readlines(0)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;Get groceries\\n&#8217;, &#8216;Organize room\\n&#8217;, &#8216;Print labels\\n&#8217;, &#8216;Write article\\n&#8217;, &#8216;Study for exam\\n&#8217;, &#8216;Learn to cookWrite to the end of the fileHi&#8217;]<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.seek(0)<\/pre>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.readlines(1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;Get groceries\\n&#8217;]<\/div>\n<h4>10. seek() in Python<\/h4>\n<p>seek() lets us reposition the cursor to the specified position.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.seek(3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<h4>11. seekable() in Python<\/h4>\n<p>This returns True if the file stream supports random access.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.seekable()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>12. tell() in Python<\/h4>\n<p>tell() tells us the current position of the cursor.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.tell()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">118<\/div>\n<h4>13. truncate() in Python<\/h4>\n<p>truncate() resizes the file. You must open your file in a writing mode for this.<\/p>\n<p>We have a To do.txt sized 118 bytes currently. Let\u2019s resize it to 100 bytes.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt','a')\r\n&gt;&gt;&gt; todo.truncate(100)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">100<\/div>\n<p>This returned the new size. We even went to our desktop and checked, it indeed resized it.<\/p>\n<p>But in doing this, it truncated some of the content of the file from the end.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt')\r\n&gt;&gt;&gt; todo.read()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Get groceries\\nOrganize room\\nPrint labels\\nWrite article\\nStudy for exam\\nLearn to cookWrite to the&#8217;<\/div>\n<p>When we provide no argument, it resizes to the current location.<\/p>\n<h4>14. writable() in Python<\/h4>\n<p>This returns True if the stream can be written to.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt')\r\n&gt;&gt;&gt; todo.writable()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>15. write(s) in Python<\/h4>\n<p>This method takes string \u2018s\u2019, and writes it to the file. Then, it returns the number of characters written.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo=open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt','a')\r\n&gt;&gt;&gt; todo.write('\\nWrite assignment')\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">17<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; todo.close()\r\n&gt;&gt;&gt; with open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt') as todo: todo.read()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Get groceries\\nOrganize room\\nPrint labels\\nWrite article\\nStudy for exam\\nLearn to cookWrite to the\\nWrite assignment&#8217;<\/div>\n<h4>16. writelines() in Python<\/h4>\n<p>writelines() writes a list of lines to a file.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; with open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt','a') as todo:<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">todo.writelines([&#8216;\\nOne&#8217;,&#8217;\\nTwo&#8217;,&#8217;\\nThree&#8217;])<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; with open('C:\\\\Users\\\\lifei\\\\Desktop\\\\To do.txt') as todo:<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">print(todo.read())Get groceries<br \/>\nOrganize room<br \/>\nPrint labels<br \/>\nWrite article<br \/>\nStudy for exam<br \/>\nLearn to cookWrite to the<br \/>\nWrite assignment<br \/>\nOne<br \/>\nTwo<br \/>\nThree<\/div>\n<p>So this was all about file handling in Python and Python read and write file.<\/p>\n<h3>Python Interview Questions on File Handling<\/h3>\n<p>1. What is file handling in Python?<\/p>\n<p>2. How do you handle a file in Python?<\/p>\n<p>3. What is the use of file handling in Python?<\/p>\n<p>4. Give an example for file handling in Python.<\/p>\n<p>5. What is stored in file handle in Python?<\/p>\n<h3>Conclusion<\/h3>\n<p>After this article, we hope you\u2019ve revised Python Read and Write File, what we saw in the previous one, and learned something new.<\/p>\n<p>If you can\u2019t understand something in File Handling in Python, ask us in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we told you about File I\/O in Python.\u00a0Today, we will see File Handling in Python, in which we study: Python Read File, Python Write File, Python Open File, and Python Close File. Along&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":8516,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[4681,10417,10536,10734,10794,10796,10928,16508],"class_list":["post-8404","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-file-handling-in-python","tag-python-close-file","tag-python-file-methods","tag-python-open-file","tag-python-read-and-write-file","tag-python-read-file","tag-python-write-to-file","tag-reading-writing-files-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>File Handling In Python - Python Read And Write File - DataFlair<\/title>\n<meta name=\"description\" content=\"File Handling in Python and Python Read and Write File - Python File Methods, Python Write to File, Python Read, Open and Close File\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"File Handling In Python - Python Read And Write File - DataFlair\" \/>\n<meta property=\"og:description\" content=\"File Handling in Python and Python Read and Write File - Python File Methods, Python Write to File, Python Read, Open and Close File\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-17T07:42:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-20T10:07:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/File-Handling-In-python-01-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"File Handling In Python - Python Read And Write File - DataFlair","description":"File Handling in Python and Python Read and Write File - Python File Methods, Python Write to File, Python Read, Open and Close File","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/","og_locale":"en_US","og_type":"article","og_title":"File Handling In Python - Python Read And Write File - DataFlair","og_description":"File Handling in Python and Python Read and Write File - Python File Methods, Python Write to File, Python Read, Open and Close File","og_url":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-02-17T07:42:03+00:00","article_modified_time":"2026-04-20T10:07:27+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/File-Handling-In-python-01-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"File Handling In Python &#8211; Python Read And Write File","datePublished":"2018-02-17T07:42:03+00:00","dateModified":"2026-04-20T10:07:27+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/"},"wordCount":1238,"commentCount":4,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/File-Handling-In-python-01-1.jpg","keywords":["File handling in python","Python Close File","Python File Methods","Python Open File","Python read and write file","Python Read File","Python Write to File","Reading &amp; Writing Files in Python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/file-handling-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/","url":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/","name":"File Handling In Python - Python Read And Write File - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/File-Handling-In-python-01-1.jpg","datePublished":"2018-02-17T07:42:03+00:00","dateModified":"2026-04-20T10:07:27+00:00","description":"File Handling in Python and Python Read and Write File - Python File Methods, Python Write to File, Python Read, Open and Close File","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/file-handling-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/File-Handling-In-python-01-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/File-Handling-In-python-01-1.jpg","width":1200,"height":628,"caption":"Python Read and Write File"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/file-handling-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"File Handling In Python &#8211; Python Read And Write File"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/8404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=8404"}],"version-history":[{"count":14,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/8404\/revisions"}],"predecessor-version":[{"id":147713,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/8404\/revisions\/147713"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/8516"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=8404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=8404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=8404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}