{"id":45111,"date":"2019-01-15T13:38:27","date_gmt":"2019-01-15T13:38:27","guid":{"rendered":"https:\/\/www.softwaretestinghelp.com\/?page_id=45111"},"modified":"2025-04-01T08:07:40","modified_gmt":"2025-04-01T08:07:40","slug":"python-file-reading-writing","status":"publish","type":"page","link":"https:\/\/www.softwaretestinghelp.com\/python\/python-file-reading-writing\/","title":{"rendered":"Python File Handling Tutorial: How to Create, Open, Read, Write"},"content":{"rendered":"<p><strong>An Intensive Look at Python File Handling Operations with Hands-on Examples:<br \/>\n<\/strong><\/p>\n<p>In the series of <a href=\"https:\/\/www.softwaretestinghelp.com\/python\/\"><strong>Python tutorial for beginners<\/strong><\/a>, we learned more about <strong><a href=\"https:\/\/www.softwaretestinghelp.com\/python\/python-string-functions\/\">Python String Functions<\/a><\/strong> in our last tutorial.<\/p>\n<p>Python provides us with an important feature for reading data from the file and writing data into a file.<\/p>\n<p>Mostly, in programming languages, all the values or data are stored in some variables which are volatile in nature.<\/p>\n<p>Because data will be stored into those variables during run-time only and will be lost once the program execution is completed. Hence it is better to save these data permanently using files.<\/p>\n<p><strong> <\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Python-File-Handling.png\"><img decoding=\"async\" class=\"alignnone wp-image-45185 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Python-File-Handling.png\" alt=\"Python File Handling Create, Open, Append, Read, Write in files\" width=\"650\" height=\"366\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Python-File-Handling.png 650w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Python-File-Handling-300x169.png 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><\/p>\n<h3><strong>How Python Handle Files<\/strong>?<\/h3>\n<p>If you are working in a large software application where they process a large number of data, then we cannot expect those data to be stored in a variable as the variables are volatile in nature.<\/p>\n<p>Hence when are you about to handle such situations, the role of files will come into the picture.<\/p>\n<p>As files are non-volatile in nature, the data will be stored permanently in a secondary device like Hard Disk and using python we will handle these files in our applications.<\/p>\n<p><em><strong>Are you thinking about how python will handle files?<\/strong> <\/em><\/p>\n<p>Let\u2019s take an <span style=\"text-decoration: underline;\"><strong>Example<\/strong><\/span> of how normal people will handle the files. If we want to read the data from a file or write the data into a file, then, first of all, we will open the file or will create a new file if the file does not exist and then perform the normal read\/write operations, save the file and close it.<\/p>\n<p>Similarly, we do the same operations in python using some in-built methods or functions.<\/p>\n<h3>Types Of File in Python<\/h3>\n<p>There are two types of files in Python and each of them are explained below in detail with examples for your easy understanding.<\/p>\n<p><strong>They are:<br \/>\n<\/strong><\/p>\n<ul>\n<li>Binary file<\/li>\n<li>Text file<\/li>\n<\/ul>\n<h4><span style=\"color: #ff6600;\">Binary files in Python<\/span><\/h4>\n<p>Most of the files that we see in our computer system are called binary files.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example:<\/strong><\/span><\/p>\n<ol>\n<li><strong>Document files:<\/strong> .pdf, .doc, .xls etc.<\/li>\n<li><strong>Image files:<\/strong> .png, .jpg, .gif, .bmp etc.<\/li>\n<li><strong>Video files:<\/strong> .mp4, .3gp, .mkv, .avi etc.<\/li>\n<li><strong>Audio files:<\/strong> .mp3, .wav, .mka, .aac etc.<\/li>\n<li><strong>Database files:<\/strong> .mdb, .accde, .frm, .sqlite etc.<\/li>\n<li><strong>Archive files:<\/strong> .zip, .rar, .iso, .7z etc.<\/li>\n<li><strong>Executable files:<\/strong> .exe, .dll, .class etc.<\/li>\n<\/ol>\n<p><em><strong>Recommended Reading =&gt;&gt; <a href=\"https:\/\/www.softwaretestinghelp.com\/how-to-open-a-7z-file\/\">How to Open .7z File<\/a><\/strong><\/em><\/p>\n<p>All binary files follow a specific format. We can open some binary files in the normal text editor but we can\u2019t read the content present inside the file. That\u2019s because all the binary files will be encoded in the binary format, which can be understood only by a computer or machine.<\/p>\n<p>For handling such binary files we need a specific type of software to open it.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong> For Example,<\/strong><\/span> You need Microsoft word software to open .doc binary files. Likewise, you need a pdf reader software to open .pdf binary files and you need a photo editor software to read the image files and so on.<\/p>\n<h4><span style=\"color: #ff6600;\">Text files in Python<\/span><\/h4>\n<p>Text files don\u2019t have any specific encoding and it can be opened in normal text editor itself.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example:<\/strong><\/span><\/p>\n<ul>\n<li><strong>Web standards:<\/strong> html, XML, CSS, JSON etc.<\/li>\n<li><strong>Source code:<\/strong> c, app, js, py, java etc.<\/li>\n<li><strong>Documents:<\/strong> txt, tex, RTF etc.<\/li>\n<li><strong>Tabular data:<\/strong> csv, tsv etc.<\/li>\n<li><strong>Configuration:<\/strong> ini, cfg, reg etc.<\/li>\n<\/ul>\n<p>In this tutorial, we will see how to handle both text as well as binary files with some classic examples.<\/p>\n<h3>Python File Handling Operations<\/h3>\n<p><strong>Most importantly there are 4 types of operations that can be handled by Python on files:<\/strong><\/p>\n<ul>\n<li>Open<\/li>\n<li>Read<\/li>\n<li>Write<\/li>\n<li>Close<\/li>\n<\/ul>\n<p><strong>Other operations include:<\/strong><\/p>\n<ul>\n<li>Rename<\/li>\n<li>Delete<\/li>\n<\/ul>\n<h4><span style=\"color: #ff6600;\">Python Create and Open a File<\/span><\/h4>\n<p>Python has an in-built function called open() to open a file.<\/p>\n<p>It takes a minimum of one argument as mentioned in the below syntax. The open method returns a file object which is used to access the write, read and other in-built methods.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre>file_object = open(file_name, mode)<\/pre>\n<p>Here, file_name is the name of the file or the location of the file that you want to open, and file_name should have the file extension included as well. Which means in <strong>test.txt<\/strong> &#8211; the term test is the name of the file and .txt is the extension of the file.<\/p>\n<p>The mode in the open function syntax will tell Python as what operation you want to do on a file.<\/p>\n<ul>\n<li><strong>&#8216;r&#8217; \u2013 Read Mode:<\/strong> Read mode is used only to read data from the file.<\/li>\n<li><strong>&#8216;w&#8217; \u2013 Write Mode:<\/strong> This mode is used when you want to write data into the file or modify it. Remember write mode overwrites the data present in the file.<\/li>\n<li><strong>&#8216;a&#8217; \u2013 Append Mode:<\/strong> Append mode is used to append data to the file. Remember data will be appended at the end of the file pointer.<\/li>\n<li><strong>&#8216;r+&#8217; \u2013 Read or Write Mode:<\/strong> This mode is used when we want to write or read the data from the same file.<\/li>\n<li><strong>&#8216;a+&#8217; \u2013 Append or Read Mode:<\/strong> This mode is used when we want to read data from the file or append the data into the same file.<\/li>\n<\/ul>\n<p><span style=\"color: #ff6600;\"><strong>Note:<\/strong><\/span> The above-mentioned modes are for opening, reading or writing text files only.<\/p>\n<p>While using binary files, we have to use the same modes with the letter <strong>&#8216;b&#8217;<\/strong> at the end. So that Python can understand that we are interacting with binary files.<\/p>\n<ul>\n<li><strong>\u2018wb\u2019 &#8211; <\/strong>Open a file for write only mode in the binary format.<\/li>\n<li><strong>\u2018rb\u2019 &#8211; <\/strong>Open a file for the read-only mode in the binary format.<\/li>\n<li><strong>\u2018ab\u2019 &#8211; <\/strong>Open a file for appending only mode in the binary format.<\/li>\n<li><strong>\u2018rb+\u2019 &#8211; <\/strong>Open a file for read and write only mode in the binary format.<\/li>\n<li><strong>\u2018ab+\u2019 &#8211; <\/strong>Open a file for appending and read-only mode in the binary format.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline; color: #000000;\"><strong>Example 1:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">fo = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cr+\u201d)<\/pre>\n<p>In the above example, we are opening the file named \u2018test.txt\u2019 present at the location \u2018C:\/Documents\/Python\/\u2019 and we are opening the same file in a read-write mode which gives us more flexibility.<\/p>\n<p><span style=\"text-decoration: underline; color: #000000;\"><strong>Example 2:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">fo = open(\u201cC:\/Documents\/Python\/img.bmp\u201d, \u201crb+\u201d)<\/pre>\n<p>In the above example, we are opening the file named \u2018img.bmp\u2019 present at the location \u201cC:\/Documents\/Python\/\u201d, But, here we are trying to open the binary file.<\/p>\n<h4><span style=\"color: #ff6600;\">Python Read From File<br \/>\n<\/span><\/h4>\n<p>In order to read a file in python, we must open the file in read mode.<\/p>\n<p><strong>There are three ways in which we can read the files in python.<\/strong><\/p>\n<ul>\n<li>read([n])<\/li>\n<li>readline([n])<\/li>\n<li>readlines()<\/li>\n<\/ul>\n<p>Here, n is the number of bytes to be read.<\/p>\n<p>First, let&#8217;s create a sample text file as shown below.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Sample_text_file.png\"><img decoding=\"async\" class=\"alignnone wp-image-45129 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Sample_text_file.png\" alt=\"Read file in Python\" width=\"262\" height=\"194\" \/><\/a><\/p>\n<p><span style=\"color: #000000;\"><strong>Now let\u2019s observe what each read method does:<\/strong><\/span><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 1:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cr\u201d)\r\nprint(my_file.read(5))<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Hello<\/p>\n<p>Here we are opening the file test.txt in a read-only mode and are reading only the first 5 characters of the file using the my_file.read(5) method.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method.png\"><img decoding=\"async\" class=\"alignnone wp-image-45130 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method.png\" alt=\"Reading File using my_file.read(5) method\" width=\"443\" height=\"91\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method.png 443w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method-300x62.png 300w\" sizes=\"(max-width: 443px) 100vw, 443px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45131 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method_output.png\" alt=\"output - Reading File using my_file.read(5) method\" width=\"275\" height=\"62\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 2:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cr\u201d)\r\nprint(my_file.read())<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Hello World<br \/>\nHello Python<br \/>\nGood Morning<\/p>\n<p>Here we have not provided any argument inside the read() function. Hence it will read all the content present inside the file.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method_example_2.png\"><img decoding=\"async\" class=\"alignnone wp-image-45132 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method_example_2.png\" alt=\"Example for Read file in Python\" width=\"424\" height=\"85\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method_example_2.png 424w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method_example_2-300x60.png 300w\" sizes=\"(max-width: 424px) 100vw, 424px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method_example_2_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45133 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_read_method_example_2_output.png\" alt=\"Output of Example for Read file in Python\" width=\"255\" height=\"100\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 3:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cr\u201d)\r\nprint(my_file.readline(2))<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>He<\/p>\n<p>This function returns the first 2 characters of the next line.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method.png\"><img decoding=\"async\" class=\"alignnone wp-image-45134 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method.png\" alt=\"Example of Reading File using readline(2) method\" width=\"422\" height=\"95\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method.png 422w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method-300x68.png 300w\" sizes=\"(max-width: 422px) 100vw, 422px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45135 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method_output.png\" alt=\"Ouput of Reading File using readline(2) method\" width=\"255\" height=\"68\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 4:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cr\u201d)\r\nprint(my_file.readline())<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Hello World<\/p>\n<p>Using this function we can read the content of the file on a line by line basis.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method_Example_4.png\"><img decoding=\"async\" class=\"alignnone wp-image-45136 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method_Example_4.png\" alt=\"Example of Reading File using readline() method\" width=\"436\" height=\"112\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method_Example_4.png 436w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method_Example_4-300x77.png 300w\" sizes=\"(max-width: 436px) 100vw, 436px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method_Example_4_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45137 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readline_method_Example_4_output.png\" alt=\"Output - Reading File using readline() method\" width=\"253\" height=\"80\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 5:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cr\u201d)\r\nprint(my_file.readlines())<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>[\u2018Hello World\\n\u2019, \u2018Hello Python\\n\u2019, \u2018Good Morning\u2019]<\/p>\n<p>Here we are reading all the lines present inside the text file including the newline characters.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readlines_method.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-45138\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readlines_method.png\" alt=\"Reading_File_using_readlines()_method\" width=\"432\" height=\"93\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readlines_method.png 432w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readlines_method-300x65.png 300w\" sizes=\"(max-width: 432px) 100vw, 432px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readlines_method_output.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-45139\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readlines_method_output.png\" alt=\"Reading_File_using_readlines()_method_output\" width=\"374\" height=\"60\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readlines_method_output.png 374w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_File_using_readlines_method_output-300x48.png 300w\" sizes=\"(max-width: 374px) 100vw, 374px\" \/><\/a><\/p>\n<p>Now let\u2019s see some more practical examples of reading a file.<\/p>\n<p><strong>Reading a specific line from a File<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">line_number = 4\r\nfo = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u2019r\u2019)\r\ncurrentline = 1\r\nfor line in fo:\r\n           if(currentline == line_number):\r\n                       print(line)\r\n                       break\r\n          currentline = currentline +1<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>How are You<\/p>\n<p>In the above example, we are trying to read only the 4<sup>th<\/sup> line from the \u2018test.txt\u2019 file using a <strong>\u201cfor loop\u201d<\/strong>.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_specific_line_from_a_file.png\"><img decoding=\"async\" class=\"alignnone wp-image-45140 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_specific_line_from_a_file.png\" alt=\"Reading specific line from a file\" width=\"409\" height=\"195\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_specific_line_from_a_file.png 409w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_specific_line_from_a_file-300x143.png 300w\" sizes=\"(max-width: 409px) 100vw, 409px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_specific_line_from_a_file_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45141 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_specific_line_from_a_file_output.png\" alt=\"Reading specific line from a file - Output\" width=\"271\" height=\"64\" \/><\/a><\/p>\n<p><strong>Reading the entire file at once<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">filename = \u201cC:\/Documents\/Python\/test.txt\u201d\r\nfilehandle = open(filename, \u2018r\u2019)\r\nfiledata = filehandle.read()\r\nprint(filedata)<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Hello World<br \/>\nHello Python<br \/>\nGood Morning<br \/>\nHow are You<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_entire_file_at_once.png\"><img decoding=\"async\" class=\"alignnone wp-image-45142 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_entire_file_at_once.png\" alt=\"Reading entire file at once\" width=\"361\" height=\"114\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_entire_file_at_once.png 361w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_entire_file_at_once-300x95.png 300w\" sizes=\"(max-width: 361px) 100vw, 361px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_entire_file_at_once_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45143 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Reading_entire_file_at_once_output.png\" alt=\"Reading entire file at once - Output\" width=\"256\" height=\"107\" \/><\/a><\/p>\n<h4><span style=\"color: #ff6600;\">Python Write to File<br \/>\n<\/span><\/h4>\n<p>In order to write data into a file, we must open the file in write mode.<\/p>\n<p>We need to be very careful while writing data into the file as it overwrites the content present inside the file that you are writing, and all the previous data will be erased.<\/p>\n<p><strong>We have two methods for writing data into a file as shown below.<br \/>\n<\/strong><\/p>\n<ul>\n<li>write(string)<\/li>\n<li>writelines(list)<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>Example 1:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cw\u201d)\r\nmy_file.write(\u201cHello World\u201d)<\/pre>\n<p>The above code writes the String \u2018Hello World\u2019 into the \u2018test.txt\u2019 file.<\/p>\n<p><strong>Before writing data to a test.txt file:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Before_writing_data_to_test_file.png\"><img decoding=\"async\" class=\"alignnone wp-image-45144 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Before_writing_data_to_test_file.png\" alt=\"Python - Before writing data to test file\" width=\"277\" height=\"155\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method.png\"><img decoding=\"async\" class=\"alignnone wp-image-45145 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method.png\" alt=\"Python - Example for write()_method\" width=\"451\" height=\"119\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method.png 451w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method-300x79.png 300w\" sizes=\"(max-width: 451px) 100vw, 451px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45146 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method_output.png\" alt=\"write()_method - Output\" width=\"280\" height=\"89\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 2:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cw\u201d)\r\nmy_file.write(\u201cHello World\\n\u201d)\r\nmy_file.write(\u201cHello Python\u201d)<\/pre>\n<p>The first line will be \u2018Hello World\u2019 and as we have mentioned \\n character, the cursor will move to the next line of the file and then write \u2018Hello Python\u2019.<\/p>\n<p>Remember if we don\u2019t mention \\n character, then the data will be written continuously in the text file like \u2018Hello WorldHelloPython\u2019<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method_with_next_line.png\"><img decoding=\"async\" class=\"alignnone wp-image-45147 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method_with_next_line.png\" alt=\"write()_method with next line\" width=\"459\" height=\"102\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method_with_next_line.png 459w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method_with_next_line-300x67.png 300w\" sizes=\"(max-width: 459px) 100vw, 459px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method_with_next_line_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45148 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method_with_next_line_output.png\" alt=\"write()_method with next line - Output\" width=\"303\" height=\"153\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method_with_next_line_output.png 303w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_data_to_file_using_write_method_with_next_line_output-300x151.png 300w\" sizes=\"(max-width: 303px) 100vw, 303px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 3:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">fruits = &#x5B;\u201cApple\\n\u201d, \u201cOrange\\n\u201d, \u201cGrapes\\n\u201d, \u201cWatermelon\u201d]\r\nmy_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cw\u201d)\r\nmy_file.writelines(fruits)<\/pre>\n<p>The above code writes a <strong>list of data<\/strong> into the \u2018test.txt\u2019 file simultaneously.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_List_of_data_to_file_using_writelines_method.png\"><img decoding=\"async\" class=\"alignnone wp-image-45149 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_List_of_data_to_file_using_writelines_method.png\" alt=\"Example for writelines()_method\" width=\"474\" height=\"112\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_List_of_data_to_file_using_writelines_method.png 474w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_List_of_data_to_file_using_writelines_method-300x71.png 300w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_List_of_data_to_file_using_writelines_method_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45150 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_List_of_data_to_file_using_writelines_method_output.png\" alt=\"writelines()_method - Output\" width=\"229\" height=\"179\" \/><\/a><\/p>\n<h4><span style=\"color: #ff6600;\">Python Append to File<\/span><\/h4>\n<p>To append data into a file we must open the file in \u2018a+\u2019 mode so that we will have access to both the append as well as write modes.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 1:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201ca+\u201d)\r\nmy_file.write (\u201cStrawberry\u201d)<\/pre>\n<p>The above code appends the string \u2018Apple\u2019 at the <strong>end<\/strong> of the \u2018test.txt\u2019 file.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_1.png\"><img decoding=\"async\" class=\"alignnone wp-image-45151 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_1.png\" alt=\"Example - Appending Data to File\" width=\"429\" height=\"87\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_1.png 429w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_1-300x61.png 300w\" sizes=\"(max-width: 429px) 100vw, 429px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_1_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45152 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_1_output.png\" alt=\"Appending Data to File - Output\" width=\"319\" height=\"187\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_1_output.png 319w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_1_output-300x176.png 300w\" sizes=\"(max-width: 319px) 100vw, 319px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 2:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201ca+\u201d)\r\nmy_file.write (\u201c\\nGuava\u201d)<\/pre>\n<p>The above code appends the string \u2018Apple\u2019 at the end of the \u2018test.txt\u2019 file <strong>in a new line<\/strong>.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_2.png\"><img decoding=\"async\" class=\"alignnone wp-image-45153 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_2.png\" alt=\"Appending data file at a new line\" width=\"423\" height=\"82\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_2.png 423w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_2-300x58.png 300w\" sizes=\"(max-width: 423px) 100vw, 423px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_2_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45154 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_2_output.png\" alt=\"Appending data file at a new line - Output\" width=\"318\" height=\"216\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_2_output.png 318w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_2_output-300x204.png 300w\" sizes=\"(max-width: 318px) 100vw, 318px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 3:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">fruits = &#x5B;\u201c\\nBanana\u201d, \u201c\\nAvocado\u201d, \u201c\\nFigs\u201d, \u201c\\nMango\u201d]\r\nmy_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201ca+\u201d)\r\nmy_file.writelines(fruits)<\/pre>\n<p>The above code <strong>appends a list of data<\/strong> into a \u2018test.txt\u2019 file.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_3.png\"><img decoding=\"async\" class=\"alignnone wp-image-45155 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_3.png\" alt=\"Append a list of data\" width=\"464\" height=\"103\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_3.png 464w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_3-300x67.png 300w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_3_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45156 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_3_output.png\" alt=\"Append a list of data - output\" width=\"315\" height=\"341\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_3_output.png 315w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_3_output-277x300.png 277w\" sizes=\"(max-width: 315px) 100vw, 315px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline; color: #000000;\"><strong>Example 4:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">text=&#x5B;&amp;quot;\\nHello&amp;quot;,&amp;quot;\\nHi&amp;quot;,&amp;quot;\\nPython&amp;quot;]\r\nmy_file=open(&amp;quot;C:\/Documents\/Python\/test.txt&amp;quot;,mode=&amp;quot;a+&amp;quot;)\r\nmy_file.writelines(text)\r\nprint(&amp;quot;where the file cursor is:&amp;quot;,my_file.tell())\r\nmy_file.seek(0)\r\nfor line in my_file:\r\n      print(line)<\/pre>\n<p>In the above code, we are appending the list of data into the \u2018test.txt\u2019 file. Here, you can observe that we have used the tell() method which prints where the cursor is currently at.<\/p>\n<p><strong>seek(offset):<\/strong> The offset takes three types of arguments namely 0,1 and 2.<br \/>\n<strong>When the offset is 0:<\/strong> Reference will be pointed at the beginning of the file.<br \/>\n<strong>When the offset is 1:<\/strong> Reference will be pointed at the current cursor position.<br \/>\n<strong>When the offset is 2:<\/strong> Reference will be pointed at the end of the file.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_4.png\"><img decoding=\"async\" class=\"alignnone wp-image-45157 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_4.png\" alt=\"Appending data using tell() method\" width=\"462\" height=\"171\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_4.png 462w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_4-300x111.png 300w\" sizes=\"(max-width: 462px) 100vw, 462px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_4_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45158 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_4_output.png\" alt=\"Appending data using tell() method - output\" width=\"270\" height=\"456\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_4_output.png 270w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Appending_data_to_file_Example_4_output-178x300.png 178w\" sizes=\"(max-width: 270px) 100vw, 270px\" \/><\/a><\/p>\n<h4><span style=\"color: #ff6600;\">Python Close File<\/span><\/h4>\n<p>In order to close a file, we must first open the file. In python, we have an in-built method called close() to close the file which is opened.<\/p>\n<p>Whenever you open a file, it is important to close it, especially, with write method. Because if we don\u2019t call the close function after the write method then whatever data we have written to a file will not be saved into the file.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 1:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cr\u201d)\r\nprint(my_file.read())\r\nmy_file.close()<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Example 2:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cw\u201d)\r\nmy_file.write(\u201cHello World\u201d)\r\nmy_file.close()<\/pre>\n<h4><span style=\"color: #ff6600;\">Python Rename or Delete File<\/span><\/h4>\n<p>Python provides us with an \u201cos\u201d module which has some in-built methods that would help us in performing the file operations such as renaming and deleting the file.<\/p>\n<p>In order to use this module, first of all, we need to import the \u201cos\u201d module in our program and then call the related methods.<\/p>\n<p><strong>rename() method:<\/strong><\/p>\n<p>This rename() method accepts two arguments i.e. the current file name and the new file name.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre>os.rename(current_file_name, new_file_name)<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Example 1:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">import os\r\nos.rename(\u201ctest.txt\u201d, \u201ctest1.txt\u201d)<\/pre>\n<p>Here \u2018test.txt\u2019 is the current file name and \u2018test1.txt\u2019 is the new file name.<\/p>\n<p>You can specify the location as well as shown in the below example.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 2:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">import os\r\nos.rename(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cC:\/Documents\/Python\/test1.txt\u201d)<\/pre>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Renaming_file.png\"><img decoding=\"async\" class=\"alignnone wp-image-45162 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Renaming_file.png\" alt=\"Python - Renaming a file\" width=\"580\" height=\"87\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Renaming_file.png 580w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Renaming_file-300x45.png 300w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><strong>Before Renaming the file:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Before_renaming_the_file.png\"><img decoding=\"async\" class=\"alignnone wp-image-45163 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Before_renaming_the_file.png\" alt=\"Python - Before renaming the file\" width=\"650\" height=\"94\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Before_renaming_the_file.png 650w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Before_renaming_the_file-300x43.png 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><\/p>\n<p><strong>After executing the above program<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/After_renaming_the_file.png\"><img decoding=\"async\" class=\"alignnone wp-image-45164 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/After_renaming_the_file.png\" alt=\"Python - After renaming the file\" width=\"645\" height=\"89\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/After_renaming_the_file.png 645w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/After_renaming_the_file-300x41.png 300w\" sizes=\"(max-width: 645px) 100vw, 645px\" \/><\/a><\/p>\n<p><strong>remove() method:<\/strong><\/p>\n<p>We use the remove() method to delete the file by supplying the file name or the file location that you want to delete.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre>os.remove(file_name)<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Example 1:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">import os\r\nos.remove(\u201ctest.txt\u201d)<\/pre>\n<p>Here \u2018test.txt\u2019 is the file that you want to remove.<\/p>\n<p>Similarly, we can pass the file location as well to the arguments as shown in the below example<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 2:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> import os\r\nos.remove(\u201cC:\/Documents\/Python\/test.txt\u201d)<\/pre>\n<h3>Encoding in Files<\/h3>\n<p>File encoding represents converting characters into a specific format which only a machine can understand.<\/p>\n<p><strong>Different machines have different encoding format as shown below.<\/strong><\/p>\n<ul>\n<li>Microsoft Windows OS uses <strong>\u2018cp1252\u2019<\/strong> encoding format by default.<\/li>\n<li>Linux or Unix OS uses <strong>\u2018utf-8\u2019<\/strong> encoding format by default.<\/li>\n<li>Apple\u2019s MAC OS uses <strong>\u2018utf-8\u2019 or \u2018utf-16\u2019<\/strong> encoding format by default.<\/li>\n<\/ul>\n<p><em><strong>Let\u2019s see the encoding operation with some examples.<\/strong><\/em><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 1:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, mode=\u201dr\u201d)\r\nprint(\u201cMicrosoft Windows encoding format by default is:\u201d, my_file.encoding)\r\nmy_file.close()<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Microsoft Windows encoding format by default is cp1252.<\/p>\n<p>Here, I executed my program on the windows machine, so it has printed the default encoding as \u2018cp1252\u2019.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_1.png\"><img decoding=\"async\" class=\"alignnone wp-image-45165 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_1.png\" alt=\"Python File Encoding with Microsoft Windows\" width=\"591\" height=\"103\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_1.png 591w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_1-300x52.png 300w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_1_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45166 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_1_output.png\" alt=\"Python File Encoding with Microsoft Windows - output\" width=\"409\" height=\"76\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_1_output.png 409w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_1_output-300x56.png 300w\" sizes=\"(max-width: 409px) 100vw, 409px\" \/><\/a><\/p>\n<p>We can also change the encoding format of a file by passing it as arguments to the open function.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 2:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, mode=\u201dw\u201d, encoding=\u201dcp437\u201d)\r\nprint(\u201cFile encoding format is:\u201d, my_file.encoding)\r\nmy_file.close()<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>File encoding format is: cp437<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_2.png\"><img decoding=\"async\" class=\"alignnone wp-image-45167 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_2.png\" alt=\"File Encoding Format cp437\" width=\"584\" height=\"111\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_2.png 584w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_2-300x57.png 300w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_2_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45168 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_2_output.png\" alt=\"File Encoding Format cp437 - output\" width=\"264\" height=\"65\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example 3:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, mode=\u201dw\u201d, encoding=\u201dutf-16\u201d)\r\nprint(\u201cFile encoding format is:\u201d, my_file.encoding)\r\nmy_file.close()<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>File encoding format is: utf-16<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_3.png\"><img decoding=\"async\" class=\"alignnone wp-image-45169 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_3.png\" alt=\"File Encoding format utf-16\" width=\"593\" height=\"99\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_3.png 593w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_3-300x50.png 300w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_3_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45170 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Encoding_the_file_example_3_output.png\" alt=\"File Encoding with utf-16 - output\" width=\"253\" height=\"70\" \/><\/a><\/p>\n<h3>Writing and Reading Data from a Binary File<\/h3>\n<p>Binary files store data in the binary format (0\u2019s and 1\u2019s) which is understandable by the machine. So when we open the binary file in our machine, it decodes the data and displays in a human-readable format.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Example:<\/strong><\/span><\/p>\n<p>#Let\u2019s create some binary file.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nmy_file = open(\u201cC:\/Documents\/Python\/bfile.bin\u201d, \u201cwb+\u201d)\r\nmessage = \u201cHello Python\u201d\r\nfile_encode = message.encode(\u201cASCII\u201d)\r\nmy_file.write(file_encode)\r\nmy_file.seek(0)\r\nbdata = my_file.read()\r\nprint(\u201cBinary Data:\u201d, bdata)\r\nntext = bdata.decode(\u201cASCII\u201d)\r\nprint(\u201cNormal data:\u201d, ntext)<\/pre>\n<p>In the above example, first we are creating a binary file <strong>\u2018bfile.bin\u2019<\/strong> with the read and write access and whatever data you want to enter into the file must be encoded before you call the write method.<\/p>\n<p>Also, we are printing the data without decoding it, so that we can observe how the data exactly looks inside the file when it\u2019s encoded and we are also printing the same data by decoding it so that it can be readable by humans.<\/p>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Binary Data: b&#8217;Hello Python&#8217;<br \/>\nNormal data: Hello Python<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_and_reading_data_from_the_binary_file.png\"><img decoding=\"async\" class=\"alignnone wp-image-45171 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_and_reading_data_from_the_binary_file.png\" alt=\"Example - Writing and Reading Data from the Binary File\" width=\"479\" height=\"198\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_and_reading_data_from_the_binary_file.png 479w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_and_reading_data_from_the_binary_file-300x124.png 300w\" sizes=\"(max-width: 479px) 100vw, 479px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_and_reading_data_from_the_binary_file_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45172 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/Writing_and_reading_data_from_the_binary_file_output.png\" alt=\"Writing and Reading Data from the Binary File - output\" width=\"262\" height=\"76\" \/><\/a><\/p>\n<h3>File I\/O Attributes<\/h3>\n\n<div id=\"tablepress-521-scroll-wrapper\" class=\"tablepress-scroll-wrapper\">\n<table id=\"tablepress-521\" class=\"tablepress tablepress-id-521 tablepress-responsive\">\n<thead>\n<tr class=\"row-1\">\n\t<th class=\"column-1\">Attribute<\/th><th class=\"column-2\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-2\">\n\t<td class=\"column-1\">Name<\/td><td class=\"column-2\">Return the name of the file<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\">Mode<\/td><td class=\"column-2\">Return mode of the file<\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\">Encoding<\/td><td class=\"column-2\">Return the encoding format of the file<\/td>\n<\/tr>\n<tr class=\"row-5\">\n\t<td class=\"column-1\">Closed<\/td><td class=\"column-2\">Return true if the file closed else returns false<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n\n<p><span style=\"text-decoration: underline; color: #000000;\"><strong>Example:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201ca+\u201d)\r\nprint(\u201cWhat is the file name? \u201d, my_file.name)\r\nprint(\u201cWhat is the file mode? \u201d, my_file.mode)\r\nprint(\u201cWhat is the encoding format? \u201d, my_file.encoding)\r\nprint(\u201cIs File closed? \u201d, my_file.closed)\r\nmy_file.close()\r\nprint(\u201cIs File closed? \u201d, my_file.closed)<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>What is the file name? C:\/Documents\/Python\/test.txt<br \/>\nWhat is the file mode? r<br \/>\nWhat is the encoding format? cp1252<br \/>\nIs File closed? False<br \/>\nIs File closed? True<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_attributes.png\"><img decoding=\"async\" class=\"alignnone wp-image-45173 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_attributes.png\" alt=\"File i\/o attributes Example 1\" width=\"467\" height=\"163\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_attributes.png 467w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_attributes-300x105.png 300w\" sizes=\"(max-width: 467px) 100vw, 467px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_attributes_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45174 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_attributes_output.png\" alt=\"File i\/o attributes - output\" width=\"383\" height=\"128\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_attributes_output.png 383w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_attributes_output-300x100.png 300w\" sizes=\"(max-width: 383px) 100vw, 383px\" \/><\/a><\/p>\n<p>Let\u2019s try out a few other methods of the file.<\/p>\n<p><span style=\"text-decoration: underline; color: #000000;\"><strong>Example:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(\u201cC:\/Documents\/Python\/test.txt\u201d, \u201cw+\u201d)\r\nmy_file.write(\u201cHello Python\\nHello World\\nGood Morning\u201d)\r\nmy_file.seek(0)\r\nprint(my_file.read())\r\nprint(\u201cIs file readable: ?\u201d, my_file.readable())\r\nprint(\u201cIs file writeable: ?\u201d, my_file.writable())\r\nprint(\u201cFile no:\u201d, my_file.fileno())\r\nmy_file.close()<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Hello Python<br \/>\nHello World<br \/>\nGood Morning<br \/>\nIs file readable:? True<br \/>\nIs file writeable:? True<br \/>\nFile no: 3<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_methods.png\"><img decoding=\"async\" class=\"alignnone wp-image-45176 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_methods.png\" alt=\"File i\/o attributes Example 2\" width=\"456\" height=\"186\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_methods.png 456w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_methods-300x122.png 300w\" sizes=\"(max-width: 456px) 100vw, 456px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_method_output.png\"><img decoding=\"async\" class=\"alignnone wp-image-45175 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/File_io_method_output.png\" alt=\"File i\/o Attributes - Example 2 output\" width=\"250\" height=\"146\" \/><\/a><\/p>\n<h3>Python File Methods<\/h3>\n\n<div id=\"tablepress-522-scroll-wrapper\" class=\"tablepress-scroll-wrapper\">\n<table id=\"tablepress-522\" class=\"tablepress tablepress-id-522 tablepress-responsive\">\n<thead>\n<tr class=\"row-1\">\n\t<th class=\"column-1\">Function<\/th><th class=\"column-2\">Explanation<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-2\">\n\t<td class=\"column-1\">open()<\/td><td class=\"column-2\">To open a file<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\">close()<\/td><td class=\"column-2\">Close an open file<\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\">fileno()<\/td><td class=\"column-2\">Returns an integer number of the file<\/td>\n<\/tr>\n<tr class=\"row-5\">\n\t<td class=\"column-1\">read(n)<\/td><td class=\"column-2\">Reads \u2018n\u2019 characters from the file till end of the file<\/td>\n<\/tr>\n<tr class=\"row-6\">\n\t<td class=\"column-1\">readable()<\/td><td class=\"column-2\">Returns true if the file is readable<\/td>\n<\/tr>\n<tr class=\"row-7\">\n\t<td class=\"column-1\">readline()<\/td><td class=\"column-2\">Read and return one line from the file<\/td>\n<\/tr>\n<tr class=\"row-8\">\n\t<td class=\"column-1\">readlines()<\/td><td class=\"column-2\">Reads and returns all the lines from the file<\/td>\n<\/tr>\n<tr class=\"row-9\">\n\t<td class=\"column-1\">seek(offset)<\/td><td class=\"column-2\">Change the cursor position by bytes as specified by the offset<\/td>\n<\/tr>\n<tr class=\"row-10\">\n\t<td class=\"column-1\">seekable()<\/td><td class=\"column-2\">Returns true if the file supports random access<\/td>\n<\/tr>\n<tr class=\"row-11\">\n\t<td class=\"column-1\">tell()<\/td><td class=\"column-2\">Returns the current file location<\/td>\n<\/tr>\n<tr class=\"row-12\">\n\t<td class=\"column-1\">writable()<\/td><td class=\"column-2\">Returns true if the file is writable<\/td>\n<\/tr>\n<tr class=\"row-13\">\n\t<td class=\"column-1\">write()<\/td><td class=\"column-2\">Writes a string of data to the file<\/td>\n<\/tr>\n<tr class=\"row-14\">\n\t<td class=\"column-1\">writelines()<\/td><td class=\"column-2\">Writes a list of data to the file<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n\n<p><strong>Let\u2019s see what we have discussed so far in an end-end program.<\/strong><\/p>\n<p><span style=\"text-decoration: underline; color: #000000;\"><strong>Example:<\/strong><\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_file = open(&amp;quot;C:\/Documents\/Python\/test.txt&amp;quot;, mode=&amp;quot;w+&amp;quot;)\r\nprint(&amp;quot;What is the file name? &amp;quot;, my_file.name)\r\nprint(&amp;quot;What is the mode of the file? &amp;quot;, my_file.mode)\r\nprint(&amp;quot;What is the encoding format?&amp;quot;, my_file.encoding)\r\n\r\ntext = &#x5B;&amp;quot;Hello Python\\n&amp;quot;, &amp;quot;Good Morning\\n&amp;quot;, &amp;quot;Good Bye&amp;quot;]\r\nmy_file.writelines(text)\r\n\r\nprint(&amp;quot;Size of the file is:&amp;quot;, my_file.__sizeof__())\r\nprint(&amp;quot;Cursor position is at byte:&amp;quot;, my_file.tell())\r\nmy_file.seek(0)\r\nprint(&amp;quot;Content of the file is:&amp;quot;, my_file.read())\r\nmy_file.close()\r\n\r\nfile = open(&amp;quot;C:\/Documents\/Python\/test.txt&amp;quot;, mode=&amp;quot;r&amp;quot;)\r\n\r\nline_number = 3\r\ncurrent_line = 1\r\ndata = 0\r\nfor line in file:\r\nif current_line == line_number:\r\ndata = line\r\nprint(&amp;quot;Data present at current line is:&amp;quot;, data)\r\nbreak\r\ncurrent_line = current_line + 1\r\n\r\nbin_file = open(&amp;quot;C:\/Documents\/Python\/bfile.exe&amp;quot;, mode=&amp;quot;wb+&amp;quot;)\r\nmessage_content = data.encode(&amp;quot;utf-32&amp;quot;)\r\nbin_file.write(message_content)\r\nbin_file.seek(0)\r\nbdata = bin_file.read()\r\nprint(&amp;quot;Binary Data is:&amp;quot;, bdata)\r\nndata = bdata.decode(&amp;quot;utf-32&amp;quot;)\r\nprint(&amp;quot;Normal Data is:&amp;quot;, ndata)\r\nfile.close()\r\nbin_file.close()<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>Output:<\/strong><\/span><\/p>\n<p>What is the file name? C:\/Documents\/Python\/test.txt<br \/>\nWhat is the mode of the file? w+<br \/>\nWhat is the encoding format? cp1252<br \/>\nSize of the file is: 192<br \/>\nCursor position is at byte: 36<br \/>\nContent of the file is: Hello Python<br \/>\nGood Morning<br \/>\nGood Bye<br \/>\nData present at the current line is: Good Bye<br \/>\nBinary Data is: b&#8217;\\xff\\xfe\\x00\\x00G\\x00\\x00\\x00o\\x00\\x00\\x00o\\x00\\x00\\x00d\\x00\\x00\\x00 \\x00\\x00\\x00B\\x00\\x00\\x00y\\x00\\x00\\x00e\\x00\\x00\\x00&#8242;<br \/>\nNormal Data is: Good Bye<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/End-End_program_example1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-45178\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/End-End_program_example1.png\" alt=\"File_io_method_output\" width=\"514\" height=\"609\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/End-End_program_example1.png 514w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/End-End_program_example1-253x300.png 253w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/End-End_program_example_output.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-45177\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/End-End_program_example_output.png\" alt=\"End-End_program_example_output\" width=\"618\" height=\"233\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/End-End_program_example_output.png 618w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2019\/01\/End-End_program_example_output-300x113.png 300w\" sizes=\"(max-width: 618px) 100vw, 618px\" \/><\/a><\/p>\n<h3>Summary<\/h3>\n<p><strong>Enlisted below are a few pointers that can be summarized from the above tutorial:<\/strong><\/p>\n<ul>\n<li>We usually use a file for storing data permanently in the secondary storage as it is non-volatile in nature, so that the data may be used in the future.<\/li>\n<li>At times in some applications we may want to read the data from a text file or binary file, so we can achieve it using the in-built functions in Python like open, read, write methods etc.<\/li>\n<li>You have to be very careful while using the write method because whatever data you write into the file will be overwritten and the old data will be lost.<\/li>\n<li>In order, to prevent overwriting of data it\u2019s better to open a file in write and append mode so that data will be appended at the end of the file.<\/li>\n<li>Remember, when you open a file in the binary mode it does not accept the encoding parameter.<\/li>\n<li>You can perform renaming and deleting on a file using the rename and remove methods from the \u201cos\u201d module\/ package.<\/li>\n<\/ul>\n<p><em><strong>We hope you enjoyed this informative tutorial on Python File Handling. Our upcoming tutorial will explain more about Python Main Function.<br \/>\n<\/strong><\/em><\/p>\n<p><strong><a href=\"https:\/\/www.softwaretestinghelp.com\/python\/python-string-functions\/\">PREV Tutorial<\/a> | <a href=\"https:\/\/www.softwaretestinghelp.com\/python\/python-main-function\/\">NEXT Tutorial<\/a><\/strong><\/p>\n\r\n\t\t\t<div id=\"daexthefup-container\"\r\n\t\t\t\tclass=\"daexthefup-container daexthefup-layout-stacked daexthefup-alignment-center\"\r\n\t\t\t\tdata-post-id=\"45111\">\r\n\r\n\t\t\t\t<div class=\"daexthefup-feedback\">\r\n\t\t\t\t\t<div class=\"daexthefup-text\">\r\n\t\t\t\t\t\t<h3 class=\"daexthefup-title\">Was this helpful?<\/h3>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<div class=\"daexthefup-buttons-container\">\r\n\t\t\t\t\t\t<div class=\"daexthefup-buttons\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-yes daexthefup-button daexthefup-button-type-icon\" data-value=\"1\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-up-cls-1{fill:#c9c9c9;}.thumb-up-cls-2{fill:#e1e1e1;}.thumb-up-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_up\">\r\n                        <path class=\"thumb-up-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-up-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"20\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,9.06l-.49-.1c-.81-.17-1.61.35-1.78,1.16l-5.3,11.74c-.17.81,3.16,1.61,3.97,1.78l1.96.41c.81.17,1.61-.35,1.78-1.16l2.18-10.27c.34-1.61-.7-3.21-2.31-3.56Z\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,20h-18.67c-.83,0-1.5.67-1.5,1.5v12c0,.83.67,1.5,1.5,1.5h16.27c.71,0,1.33-.5,1.47-1.21l2.4-12c.19-.93-.53-1.8-1.47-1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-no daexthefup-button daexthefup-button-type-icon\" data-value=\"0\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-down-cls-1{fill:#c9c9c9;}.thumb-down-cls-2{fill:#e1e1e1;}.thumb-down-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_down\">\r\n                        <path class=\"thumb-down-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-down-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"13\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,38.94l-.49.1c-.81.17-1.61-.35-1.78-1.16l-5.3-11.74c-.17-.81,3.16-1.61,3.97-1.78l1.96-.41c.81-.17,1.61.35,1.78,1.16l2.18,10.27c.34,1.61-.7,3.21-2.31,3.56Z\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,28h-18.67c-.83,0-1.5-.67-1.5-1.5v-12c0-.83.67-1.5,1.5-1.5h16.27c.71,0,1.33.5,1.47,1.21l2.4,12c.19.93-.53,1.8-1.47,1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-comment\">\r\n\t\t\t\t\t<div class=\"daexthefup-comment-top-container\">\r\n\t\t\t\t\t\t<label id=\"daexthefup-comment-label\" class=\"daexthefup-comment-label\"><\/label>\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-container\">\r\n\t\t\t\t\t\t\t\t<div id=\"daexthefup-comment-character-counter-number\"\r\n\t\t\t\t\t\t\t\t\tclass=\"daexthefup-comment-character-counter-number\"><\/div>\r\n\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-text\"><\/div>\r\n\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<textarea id=\"daexthefup-comment-textarea\" class=\"daexthefup-comment-textarea\"\r\n\t\t\t\t\t\t\t\tplaceholder=\"Type your message\"\r\n\t\t\t\t\t\t\t\tmaxlength=\"\r\n\t\t\t\t\t\t\t\t400\t\t\t\t\t\t\t\t\t\"><\/textarea>\r\n\t\t\t\t\t<div class=\"daexthefup-comment-buttons-container\">\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-submit daexthefup-button\">Submit<\/button>\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-cancel daexthefup-button\">Cancel<\/button>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-successful-submission-text\">Thanks for your feedback!<\/div>\r\n\r\n\t\t\t<\/div>\r\n\r\n\t\t\t","protected":false},"excerpt":{"rendered":"<p>An Intensive Look at Python File Handling Operations with Hands-on Examples: In the series of Python tutorial for beginners, we learned more about Python String Functions in our last tutorial. Python provides us with an important feature for reading data from the file and writing data into a file. Mostly, &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Python File Handling Tutorial: How to Create, Open, Read, Write\" class=\"read-more button\" href=\"https:\/\/www.softwaretestinghelp.com\/python\/python-file-reading-writing\/#more-45111\" aria-label=\"Read more about Python File Handling Tutorial: How to Create, Open, Read, Write\">Read more<\/a><\/p>\n","protected":false},"author":9,"featured_media":45185,"parent":42278,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_helpful_pro_status":1,"footnotes":""},"categories":[407],"tags":[],"class_list":{"0":"post-45111","1":"page","2":"type-page","3":"status-publish","4":"has-post-thumbnail","6":"category-python"},"acf":[],"_links":{"self":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/45111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/comments?post=45111"}],"version-history":[{"count":0,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/45111\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/42278"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media\/45185"}],"wp:attachment":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media?parent=45111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/categories?post=45111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/tags?post=45111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}