{"id":79293,"date":"2020-06-29T09:00:37","date_gmt":"2020-06-29T03:30:37","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=79293"},"modified":"2020-06-29T09:00:37","modified_gmt":"2020-06-29T03:30:37","slug":"python-file-read-write","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/","title":{"rendered":"Python Read File and Python Write to File"},"content":{"rendered":"<p>In all your previous programs, you might have <strong>dealt<\/strong> with some <strong>sort<\/strong> of <strong>data<\/strong>. You might have asked the <strong>user<\/strong> for some <strong>data<\/strong> as <strong>input<\/strong>. Your program then might have <strong>processed<\/strong> it and <strong>produced<\/strong> some <strong>output data<\/strong> on the <strong>screen<\/strong>. But, one thing worth noticing is that as soon as your <strong>program finishes<\/strong> its <strong>execution<\/strong>, you <strong>lose<\/strong> this <strong>data<\/strong>.<\/p>\n<p>In real life applications though, you\u2019ll need to <strong>safely store<\/strong> your <strong>data<\/strong> somewhere.<\/p>\n<p>In this article, you\u2019ll learn where you can <strong>store<\/strong> this <strong>data. <\/strong>You\u2019ll learn how you can <strong>store<\/strong>, <strong>retrieve<\/strong> as well as <strong>manipulate<\/strong> this <strong>data<\/strong>. In other words, you will learn <strong>Python read file<\/strong>, <strong>Python write to file<\/strong>, <strong>Python open file <\/strong>and <strong>python close file<\/strong>.<\/p>\n<h3>What is Python File?<\/h3>\n<p>When we <strong>talked about storing our data somewhere, we meant storing our data into files.<\/strong><\/p>\n<p><strong>A file is a container<\/strong> that <strong>stores information<\/strong>, right? In computer systems, a <strong>file<\/strong> is a <strong>contiguous<\/strong> <strong>set<\/strong> of <strong>bytes. <\/strong><strong>Data<\/strong> in a <strong>computer system<\/strong> is always <strong>stored<\/strong> into <strong>files<\/strong>. Files can take different forms depending on the user requirements like <strong>data files<\/strong>, <strong>text files<\/strong>, <strong>program executable files<\/strong> etc.<\/p>\n<p>A computer processes these files by <strong>translating<\/strong> them into <strong>0s<\/strong> and <strong>1s<\/strong>.<\/p>\n<p>So all the <strong>text<\/strong>, <strong>images<\/strong>, <strong>videos<\/strong> that you store on your computer are nothing but a <strong>series<\/strong> of <strong>0s<\/strong> and <strong>1s<\/strong>.<\/p>\n<p>Every file contains three main parts:<\/p>\n<p><strong>1. Header:<\/strong> This contains information about the file i.e. <strong>file name<\/strong>, <strong>file type<\/strong>, <strong>file size<\/strong> etc.<br \/>\n<strong>2. Data:<\/strong> This is the actual <strong>information\/content<\/strong> stored in the file.<br \/>\n<strong>3. End of file:<\/strong> This is a <strong>special character<\/strong> that marks the <strong>end<\/strong> of the <strong>file<\/strong>.<\/p>\n<p>Also, in a file, a<strong> new line character (\u2018\\n\u2019)<\/strong> marks the <strong>end<\/strong> of a line or <strong>start<\/strong> of a new line.<\/p>\n<h3>Python Open File<\/h3>\n<p>You can <strong>open file<\/strong> in Python using the<strong> built-in function open()<\/strong>.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">open(filename, access_mode)<\/pre>\n<h3><strong>Parameters<\/strong><\/h3>\n<p><strong>1. filename:<\/strong><\/p>\n<p>This is the <strong>name<\/strong> of the <strong>file<\/strong> we want to <strong>open<\/strong>. If the file <strong>resides<\/strong> in the <strong>same directory<\/strong> as your <strong>program<\/strong>, you can simply <strong>specify<\/strong> the <strong>file name<\/strong>. But if the file is <strong>not present<\/strong> in the <strong>same directory<\/strong>, you need to <strong>specify<\/strong> the <strong>full path<\/strong> of the <strong>file<\/strong>.<\/p>\n<p><strong>2. access_mode:<\/strong><\/p>\n<p>Access mode <strong>specifies<\/strong> what you <strong>want to do<\/strong> with the <strong>file<\/strong>. The <strong>default<\/strong> mode is <strong>read mode \u2018r\u2019<\/strong>. You\u2019ll find a list of all the access modes later in this article.<\/p>\n<h3>Returns<\/h3>\n<p>The <strong>open() function<\/strong> returns a file object, also called <strong>\u201chandle\u201d<\/strong>. Python treats the file as an <strong>object<\/strong> and we use this <strong>file object<\/strong> in our program to <strong>access<\/strong> the <strong>contents<\/strong> of the file.<\/p>\n<p>Let\u2019s look at an example.<\/p>\n<p>We have an already existing file in our system &#8211; <strong>\u201cmyfile.txt\u201d<\/strong>. Let\u2019s open this file in the <strong>Python shell<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; file_obj = open(\"myfile.txt\", \"r\")   # file in the same directory\n&gt;&gt;&gt; file_obj = open(\"E:\/folder\/file.txt\")  # specify full path if not in the same directory\n<\/pre>\n<h3>Python Access Modes<\/h3>\n<p>The second argument to <strong>open()<\/strong> is the <strong>access_mode<\/strong>. The access_mode <strong>specifies<\/strong> the <strong>mode<\/strong> in which you want to <strong>open<\/strong> the <strong>file<\/strong>. Modes include<strong> \u201cr\u201d<\/strong> for <strong>reading<\/strong>, <strong>\u201cw\u201d<\/strong> for <strong>writing<\/strong> and<strong> \u201ca\u201d<\/strong> for <strong>appending<\/strong>. This argument is <strong>optional<\/strong> and takes the <strong>default value<\/strong> of<strong> \u201cr\u201d<\/strong>, i.e., the <strong>read mode<\/strong> if we pass <strong>no value<\/strong> to it.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Mode<\/b><\/td>\n<td><b>Function<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>r<\/b><\/td>\n<td><span style=\"font-weight: 400\">Open a file for reading. This is the default mode.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>w<\/b><\/td>\n<td><span style=\"font-weight: 400\">Open a file for writing. If the file already exists, overwrite its contents. Create a new file if the file does not exist.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>a<\/b><\/td>\n<td><span style=\"font-weight: 400\">Open a file for appending. Preserve the file\u2019s contents, add new data to the end of the file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>r+<\/b><\/td>\n<td><span style=\"font-weight: 400\">Open a file for reading and writing.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>w+<\/b><\/td>\n<td><span style=\"font-weight: 400\">Allows to write as well as read from the file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>a+<\/b><\/td>\n<td><span style=\"font-weight: 400\">Allows appending as well as reading from the file.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>By default, <strong>open() function<\/strong> opens a file in<strong> text mode<\/strong>.<\/p>\n<p>We can specify <strong>binary mode<\/strong> by adding <strong>\u201cb\u201d<\/strong> to any of the modes (e.g., <strong>\u201cwb\u201d<\/strong> &#8211; <strong>write to binary data<\/strong>, <strong>\u201crb+\u201d<\/strong> &#8211; <strong>read<\/strong> <strong>and write to binary<\/strong> <strong>data<\/strong>).<\/p>\n<h3>Python Close File<\/h3>\n<p>Many programmers often forget to <strong>close a file<\/strong> after they\u2019re done <strong>processing<\/strong> it. This may lead to <strong>unwanted data loss<\/strong> and <strong>data corruption<\/strong>. Closing a file also helps in <strong>freeing<\/strong> <strong>up<\/strong> all the <strong>resources<\/strong> tied to your program for working with the file.<\/p>\n<p>Therefore, it is always a good practice to <strong>close<\/strong> your <strong>file<\/strong> once you\u2019re done working with it. We do this using a simple<strong> in-built Python function<\/strong> &#8211; <strong>close()<\/strong>. We just need to <strong>call<\/strong> the <strong>close()<\/strong> method on the <strong>file object<\/strong> and Voila! The file is now closed.<\/p>\n<p><strong> No data lost<\/strong>, <strong>no resources left<\/strong> still <strong>tied<\/strong> to the <strong>file<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; file_obj = open(\"myfile.txt\", \"r\")\n&gt;&gt;&gt; file_obj.close()\n<\/pre>\n<h3>Python Read Data from a File<\/h3>\n<p>Let\u2019s now come to the real and fun part: <strong>operating<\/strong> on the <strong>file<\/strong>.<\/p>\n<p>Python provides us with various functions to <strong>read<\/strong> from a <strong>file<\/strong>. One way to <strong>read individual lines<\/strong> from a file without using any function is by using the <strong>\u201cfor\u201d loop<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f = open(\"myfile.txt\")\n&gt;&gt;&gt; for line in f:\n        print(line)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\n<p>Hello world! This is line 1.This is the 2nd line.<\/p>\n<p>And this is line 3.<\/p>\n<p>&gt;&gt;&gt;<\/p>\n<\/div>\n<p>This <strong>loops<\/strong> over the lines in <strong>myfile.txt<\/strong> and <strong>prints <\/strong>them <strong>one<\/strong> <strong>by one<\/strong>.<\/p>\n<p><strong>The functions we have at our disposal to read from python file are:<\/strong><\/p>\n<h3>1. read(size = -1)<\/h3>\n<p>This read <strong>size number<\/strong> of <strong>bytes<\/strong> from the <strong>file<\/strong>. If <strong>size<\/strong> is <strong>not passed<\/strong>, the <strong>entire file<\/strong> is <strong>read<\/strong>.<\/p>\n<p>Follow along through the session at <strong>Python shell<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.read(5)      # first call\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\"><span style=\"font-weight: 400\">&#8216;Hello&#8217;<\/span><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.read(10)     # second call<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\"><span style=\"font-weight: 400\">&#8216; world! Th&#8217;<\/span><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.read()      # third call<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\n<p><span style=\"font-weight: 400\">&#8216;is is line 1.\\nThis is the 2nd line.\\nAnd this is line 3.\\n&#8217;<\/span><span style=\"font-weight: 400\">&gt;&gt;&gt; <\/span><\/p>\n<\/div>\n<p>The first call to<strong> read() <\/strong>reads the first 5 bytes as 5 was passed to the <strong>size argument<\/strong>. The second call reads next 10 bytes. And the third call reads the rest of the file as we provide <strong>no size argument<\/strong>.<\/p>\n<h3>2. readline(size = -1)<\/h3>\n<p>This reads size <strong>number of bytes<\/strong> from the <strong>line<\/strong>. If we pass <strong>no argument value<\/strong>, it <strong>reads<\/strong> the <strong>entire line<\/strong>.<\/p>\n<p>Look at the example code in the Python shell:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.readline()\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><span style=\"font-weight: 400\">&#8216;Hello world! This is line 1.\\n&#8217;<\/span><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.readline()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\"><span style=\"font-weight: 400\">&#8216;This is the 2nd line.\\n&#8217;<\/span><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.readline()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\"><strong><span style=\"font-weight: 400\">&#8216;And this is line 3.\\n&#8217;<\/span><\/strong><\/div>\n<p>Notice how the first call to <strong>readline<\/strong> returns 1st line, the second call returns 2nd line and so on.<\/p>\n<h3>3. readlines()<\/h3>\n<p>This function reads all the lines from a <strong>file<\/strong> and <strong>returns<\/strong> a <strong>list<\/strong> of the <strong>lines<\/strong>, <strong>separating<\/strong> them from one another with <strong>commas<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.readlines()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;Hello world! This is line 1.\\n&#8217;, &#8216;This is the 2nd line.\\n&#8217;, &#8216;And this is line 3.\\n&#8217;]<\/div>\n<h3>Python Writing data into a File<\/h3>\n<p><strong>Writing<\/strong> into a file is just as easy as <strong>reading<\/strong> from it. All we need to do is <strong>open a file<\/strong> in <strong>write mode<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f = open(\"myfile.txt\" , \"w\")<\/pre>\n<p>Then the <strong>in-built function<\/strong> for <strong>writing<\/strong> into the <strong>file object<\/strong> will take care of the rest.<\/p>\n<h3>1. write(string)<\/h3>\n<p>This <strong>function<\/strong> takes a <strong>string<\/strong> as an <strong>argument<\/strong> and <strong>writes<\/strong> it into the <strong>file<\/strong>. This function returns the <strong>number of characters<\/strong> written into the file.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.write(\"I am writing new text into the file\\n\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">37<\/div>\n<h3>2. writelines(list_of_strings)<\/h3>\n<p>This function takes a <strong>list of strings<\/strong> as an <strong>argument<\/strong> and <strong>writes<\/strong> those <strong>strings<\/strong> into the <strong>file<\/strong>. Be sure to append a<strong> \u201c\\n\u201d<\/strong> at the <strong>end of a string<\/strong> to make it act as a <strong>line<\/strong>. The function <strong>does not append<\/strong> any <strong>line endings<\/strong> to the elements of the list of strings.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; lines = [\"Second line\\n\", \"Third line\\n\", \"Fourth line\\n\"]\n&gt;&gt;&gt; f.writelines(lines)\n&gt;&gt;&gt; f.close()\n<\/pre>\n<p>Once you <strong>close<\/strong> the <strong>file<\/strong>, all <strong>changes<\/strong> get <strong>committed<\/strong> to it.<\/p>\n<p>You can see the changes in the file:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/output-1-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-79318\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/output-1-1.png\" alt=\"python alter file\" width=\"531\" height=\"187\" \/><\/a><\/p>\n<h3>Python File Pointer Position<\/h3>\n<p>Python provides us with<strong> 2 in-built methods<\/strong> when it comes to working with the <strong>file pointer<\/strong>.<\/p>\n<h3>1. tell()<\/h3>\n<p>The<strong> tell()<\/strong> method <strong>returns<\/strong> the <strong>current position<\/strong> of the <strong>file pointer<\/strong>. That is, it returns the <strong>number of bytes<\/strong> from the <strong>beginning<\/strong> of the <strong>file<\/strong>.<\/p>\n<h3>2. seek(offset, whence)<\/h3>\n<p>The<strong> seek method<\/strong> lets you <strong>control<\/strong> the <strong>position<\/strong> of the <strong>file pointer<\/strong>. This method takes <strong>two parameters<\/strong>:<\/p>\n<p><strong>a. offset &#8211;<\/strong> The <strong>number<\/strong> of positions(bytes) to move <strong>forward<\/strong>.<\/p>\n<p><strong>b. whence &#8211;<\/strong> This is <strong>optional<\/strong>. This denotes the <strong>position<\/strong> from where you want to move<strong> forward<\/strong>. It can take <strong>three values<\/strong>:<\/p>\n<ul>\n<li>0: move forward from the <strong>start<\/strong> of the file.<\/li>\n<li>1: seek relative to the <strong>current<\/strong> position.<\/li>\n<li>2: move backwards from the <strong>end<\/strong> of the file.<\/li>\n<\/ul>\n<p>Now, look at this session of Python shell.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.readline()\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\"><span style=\"font-weight: 400\">&#8216;I am writing a new line to the file \\n&#8217;<\/span><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.tell()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\"><span style=\"font-weight: 400\">38<\/span><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.seek(20)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\"><span style=\"font-weight: 400\">20<\/span><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.tell()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\"><span style=\"font-weight: 400\">20<\/span><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f.read()\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\n<p><span style=\"font-weight: 400\">&#8216;ine to the file \\nSecond line\\nThird line\\nFourth line\\n&#8217;<\/span><span style=\"font-weight: 400\">&gt;&gt;&gt;<\/span><\/p>\n<\/div>\n<h3>Python File Methods<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Method<\/b><\/td>\n<td><b>What it does<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">close()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Closes the file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">flush()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Flushes the internal buffer.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">fileno()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Returns the file descriptor of the file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">next()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Returns the next line from the file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">read(size)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Reads size number of bytes from the file. Reads the entire file if you don\u2019t pass any argument value.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">readline(size)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Reads one line from a file.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">readlines()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Reads the entire file and returns a list of the lines.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">seek(offset, whence)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Lets us control the position of the file pointer.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">tell()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Returns the current position of the file pointer.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">truncate(size)<\/span><\/td>\n<td><span style=\"font-weight: 400\">It truncates the file to the specified size.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">writable()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Returns True if we can write into the file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">write(string)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Writes string into the file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">writelines(list_of_strings)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Writes each element of the list_of_strings into the file.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Summary<\/h3>\n<p>In this article, we learned what files are and how we can <strong>access<\/strong> and <strong>manipulate<\/strong> them. We saw Python <strong>read file<\/strong>, <strong>write file<\/strong>, <strong>Open file<\/strong> and <strong>close file<\/strong>.<\/p>\n<p><strong>Storing<\/strong> your <strong>program\u2019s data<\/strong> into a file <strong>increases<\/strong> the <strong>application<\/strong> and <strong>usability<\/strong> of your <strong>program<\/strong>. The <strong>utility<\/strong> of your application is also <strong>increased<\/strong> when you are able to get <strong>data<\/strong> from a <strong>file<\/strong> into your <strong>program<\/strong>. Now that you know how to work with files <strong>(open, process and close them)<\/strong>, you can build better programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In all your previous programs, you might have dealt with some sort of data. You might have asked the user for some data as input. Your program then might have processed it and produced&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79312,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[2985,2986,2987,2988],"class_list":["post-79293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python-close-file","tag-python-open-file","tag-python-read-file","tag-python-write-to-file"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Read File and Python Write to File - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn Python File I\/O with syntax and examples - Python read file, Python write file, Open file in Python, Close file in Python\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Read File and Python Write to File - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn Python File I\/O with syntax and examples - Python read file, Python write file, Open file in Python, Close file in Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-29T03:30:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Python-file-input-output.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Read File and Python Write to File - TechVidvan","description":"Learn Python File I\/O with syntax and examples - Python read file, Python write file, Open file in Python, Close file in Python","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:\/\/techvidvan.com\/tutorials\/python-file-read-write\/","og_locale":"en_US","og_type":"article","og_title":"Python Read File and Python Write to File - TechVidvan","og_description":"Learn Python File I\/O with syntax and examples - Python read file, Python write file, Open file in Python, Close file in Python","og_url":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-06-29T03:30:37+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Python-file-input-output.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Read File and Python Write to File","datePublished":"2020-06-29T03:30:37+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/"},"wordCount":1583,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Python-file-input-output.jpg","keywords":["Python Close File","Python Open File","Python Read File","Python Write to File"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/","url":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/","name":"Python Read File and Python Write to File - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Python-file-input-output.jpg","datePublished":"2020-06-29T03:30:37+00:00","description":"Learn Python File I\/O with syntax and examples - Python read file, Python write file, Open file in Python, Close file in Python","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Python-file-input-output.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Python-file-input-output.jpg","width":802,"height":420,"caption":"Python read file"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-file-read-write\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Read File and Python Write to File"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=79293"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79312"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=79293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=79293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=79293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}