{"id":54,"date":"2023-03-30T04:56:55","date_gmt":"2023-03-30T04:56:55","guid":{"rendered":"http:\/\/www.programminginpython.com\/2023\/03\/30\/how-to-perform-file-i-o-operations-in-python\/"},"modified":"2023-04-20T16:17:15","modified_gmt":"2023-04-20T16:17:15","slug":"how-to-perform-file-io-operations-in-python","status":"publish","type":"post","link":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/","title":{"rendered":"How to perform File IO operations in Python"},"content":{"rendered":"<p>Hello guys, welcome back to <a href=\"http:\/\/programminginpython.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">programminginpython.com<\/a>! Here in this post, I will show you how to perform different File IO operations in Python, like opening a file, reading content from the file, writing data to a file, and all the stuff. You can do most of the operations I just mentioned by using a <code>file<\/code> object.<\/p>\n<ul>\n<li><a href=\"#openFile\">Opening a file<\/a><\/li>\n<li><a href=\"#fileAttributes\">File Attributes<\/a><\/li>\n<li><a href=\"#writeToFile\">Writing to a file<\/a><\/li>\n<li><a href=\"#readFromFile\">Reading from a file<\/a><\/li>\n<li><a href=\"#appendToFile\">Appending content to a file<\/a><\/li>\n<li><a href=\"#deleteFile\">Deleting a file<\/a><\/li>\n<\/ul>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/file_operations.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<div class=\"yrc-shell-cover yrc-single\" data-yrc-uid=\"69e0fe4f866f7\" data-yrc-channel=\"{&quot;meta&quot;:{&quot;user&quot;:&quot;Programming In Python&quot;,&quot;channel&quot;:&quot;UCLBlXUMLYLxopRljNdLXOeQ&quot;,&quot;key&quot;:&quot;yrc_1683138754&quot;,&quot;apikey&quot;:&quot;AIzaSyC3vU6rFyzsO0shrRsAWmjuSiuWYTQhafc&quot;,&quot;cache&quot;:&quot;1440&quot;,&quot;channel_uploads&quot;:&quot;UULBlXUMLYLxopRljNdLXOeQ&quot;,&quot;onlyonce&quot;:&quot;&quot;,&quot;tag&quot;:&quot;&quot;,&quot;per_page&quot;:&quot;24&quot;,&quot;maxv&quot;:&quot;0&quot;,&quot;consent&quot;:{&quot;ask&quot;:&quot;&quot;,&quot;url&quot;:&quot;&quot;},&quot;ads&quot;:&quot;1&quot;,&quot;uid&quot;:&quot;69e0fe4f866f7&quot;,&quot;nocookie&quot;:&quot;&quot;,&quot;single&quot;:true},&quot;style&quot;:{&quot;colors&quot;:{&quot;item&quot;:{&quot;background&quot;:&quot;inherit&quot;},&quot;button&quot;:{&quot;background&quot;:&quot;#333&quot;,&quot;color&quot;:&quot;#fff&quot;},&quot;color&quot;:{&quot;text&quot;:&quot;#fff&quot;,&quot;link&quot;:&quot;inherit&quot;,&quot;menu&quot;:&quot;#000&quot;,&quot;meta&quot;:&quot;inherit&quot;}},&quot;theme&quot;:{&quot;videos&quot;:{&quot;style&quot;:&quot;__grid&quot;,&quot;thumb&quot;:[&quot;large&quot;,&quot;open&quot;],&quot;desc&quot;:&quot;&quot;,&quot;carousel&quot;:{&quot;thumbs&quot;:&quot;4&quot;,&quot;thumbs_to_slide&quot;:&quot;2&quot;,&quot;spacing&quot;:&quot;10&quot;},&quot;carousel_nav&quot;:{&quot;modifier&quot;:&quot;__sides&quot;,&quot;position&quot;:&quot;left-none&quot;,&quot;location&quot;:&quot;prepend&quot;,&quot;background&quot;:&quot;#fff&quot;,&quot;color&quot;:&quot;#000&quot;,&quot;font_size&quot;:&quot;2&quot;,&quot;border_radius&quot;:&quot;0&quot;}},&quot;a&quot;:&quot;1&quot;},&quot;fit&quot;:&quot;false&quot;,&quot;playlists&quot;:&quot;true&quot;,&quot;uploads&quot;:&quot;true&quot;,&quot;player_mode&quot;:&quot;1&quot;,&quot;truncate&quot;:&quot;&quot;,&quot;banner&quot;:&quot;true&quot;,&quot;thumb_margin&quot;:&quot;8&quot;,&quot;play_icon&quot;:&quot;hover&quot;,&quot;youtube_play_icon&quot;:&quot;&quot;,&quot;thumb_image_size&quot;:&quot;medium&quot;,&quot;default_tab&quot;:&quot;uploads&quot;,&quot;sticky&quot;:{&quot;enable&quot;:&quot;&quot;,&quot;width&quot;:&quot;400&quot;,&quot;position&quot;:&quot;bottom-right&quot;,&quot;only_above&quot;:&quot;768&quot;,&quot;margin&quot;:&quot;12&quot;},&quot;player&quot;:{&quot;show_desc&quot;:&quot;&quot;,&quot;show_meta&quot;:&quot;&quot;},&quot;menu&quot;:&quot;1&quot;,&quot;rating_style&quot;:&quot;NaN&quot;,&quot;rtl&quot;:false}}\" data-yrc-setup=\"\"><\/div>\r\n\t\t\t<script data-cfasync=\"false\" type=\"text\/javascript\">\r\n\t\t\t\tif( !window.YRC ) var YRC = {Data:{}};\r\n\t\t\t\tYRC.Data[\"69e0fe4f866f7\"] = {\"video\":\"56tuiPd3SIA\"};\r\n\t\t\t\t(function(){\r\n\t\t\t\t\tif(!YRC.loaded){\r\n\t\t\t\t\t    YRC.loaded = true;\r\n\t\t\t\t\t\tfunction YRC_Loader(){\r\n\t\t\t\t\t\t\t\/\/YRC.loaded = true;\r\n\t\t\t\t\t\t\tYRC.is_pro = false;\r\n\t\t\t\t\t\t\tYRC.is_pb = false;\r\n\t\t\t\t\t\t\tYRC.lang = {\"form\":{\"Videos\":\"Videos\",\"Playlists\":\"Playlists\",\"Search\":\"Search\",\"Loading\":\"Loading\",\"more\":\"more\",\"Nothing_found\":\"Nothing found\",\"Prev\":\"Previous\",\"Next\":\"Next\",\"consent_statement\":\"Allow cookies?\",\"consent_button\":\"Allow\",\"consent_privacy_policy\":\"Privacy policy\"},\"fui\":{\"sort_by\":\"Sort by\",\"relevant\":\"Relevant\",\"latest\":\"Latest\",\"liked\":\"Liked\",\"title\":\"Title\",\"views\":\"Views\",\"duration\":\"Duration\",\"any\":\"Any\",\"_short\":\"Short\",\"medium\":\"Medium\",\"_long\":\"Long\",\"uploaded\":\"Uploaded\",\"all_time\":\"All time\",\"live_now\":\"Live Now\",\"today\":\"Today\",\"ago\":\"ago\",\"last\":\"Last\",\"day\":\"day\",\"days\":\"days\",\"week\":\"week\",\"weeks\":\"weeks\",\"month\":\"month\",\"months\":\"months\",\"year\":\"year\",\"years\":\"years\",\"older\":\"Older\",\"show_more\":\"Show More\",\"show_less\":\"Show Less\",\"reply\":\"REPLY\",\"view_replies\":\"View replies\",\"write_comment\":\"Write comment...\",\"billion\":\"B\",\"million\":\"M\",\"thousand\":\"K\",\"max_plain_number\":1000,\"wplocale\":\"en_US\"}};\r\n\t\t\t\t\t\t\tYRC.is_admin = false;\r\n\t\t\t\t\t\t\tvar script = document.createElement(\"script\");\r\n\t\t\t\t\t\t\t\tscript.setAttribute(\"data-cfasync\", \"false\");\r\n\t\t\t\t\t\t\t\tscript.setAttribute(\"type\", \"text\/javascript\");\r\n\t\t\t\t\t\t\t\tscript.src = \"https:\/\/www.programminginpython.com\/wp-content\/plugins\/yourchannel\/js\/yrc.js?1.2.9\";\r\n\t\t\t\t\t\t\t\tscript.id = \"yrc-script\";\r\n\t\t\t\t\t\t\t\tdocument.querySelector(\"head\").appendChild(script);\r\n\t\t\t\t\t\t\tvar style = document.createElement(\"link\");\r\n\t\t\t\t\t\t\t\tstyle.rel = \"stylesheet\";\r\n\t\t\t\t\t\t\t\tstyle.href = \"https:\/\/www.programminginpython.com\/wp-content\/plugins\/yourchannel\/css\/style.css?1.2.9\";\r\n\t\t\t\t\t\t\t\tstyle.type = \"text\/css\";\r\n\t\t\t\t\t\t\t\tdocument.querySelector(\"head\").appendChild(style);\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tif(window.jQuery){YRC_Loader();}else { var yrctimer2324 = window.setInterval(function(){\r\n\t\t\t\t\t\t\tif(window.jQuery){YRC_Loader(); window.clearInterval(yrctimer2324); }\r\n\t\t\t\t\t\t}, 250);}\r\n\t\t\t\t\t} else {if(YRC.EM)YRC.EM.trigger(\"yrc.newchannel\");}\r\n\t\t\t\t}());<\/script>\n<p style=\"text-align: center;\"><strong>Check this video on YouTube <a href=\"https:\/\/youtu.be\/56tuiPd3SIA\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/strong><\/p>\n<h4 id=\"openFile\" class=\"post_h4\">Opening a file<\/h4>\n<p>If you want to perform any operations like reading, appending, or writing, first, you must open a file. You can open a file by using Python\u2019s built-in function <code>open()<\/code>. This <code>open()<\/code> the function takes 3 arguments, they are the <code>file name<\/code>,\u00a0<code>access mode<\/code> and <code>buffering<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">some_file_object = open(file_name access_mode, buffering)<\/pre>\n<p>The 3 parameters,<\/p>\n<p><code>file_name<\/code> \u2013 This parameter contains the name of the file which you want to access or perform some operations on it.<\/p>\n<p><code>access_mode<\/code> \u2013 This access_mode determines in which mode the file should open, i.e reading(\u2018<code>r<\/code>\u2018), writing(\u2018<code>w<\/code>\u2018), appending(\u2018<code>a<\/code>\u2018), reading and writing(\u2018<code>r+<\/code>\u2018), reading in binary format(\u2018<code>rb<\/code>\u2018) and many other modes. Below is a list of all the access modes.<\/p>\n<figure id=\"attachment_629\" class=\"wp-caption aligncenter\" style=\"width: 782px;\" aria-describedby=\"caption-attachment-629\">\n<p><figure id=\"attachment_531\" aria-describedby=\"caption-attachment-531\" style=\"width: 782px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-531 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_file_modes-1.png\" alt=\"File IO Operations in Python\" width=\"782\" height=\"288\" data-srcset=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_file_modes-1.png 782w, https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_file_modes-1.png 300w, https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_file_modes-1.png 768w\" data-sizes=\"(max-width: 640px) 100vw, 640px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 782px; --smush-placeholder-aspect-ratio: 782\/288;\" \/><figcaption id=\"caption-attachment-531\" class=\"wp-caption-text\">File IO Operations in Python<\/figcaption><\/figure><\/figure>\n<p><code>buffering<\/code> \u2013 This buffering is an optional parameter if you want a buffering option you set its value to 1, else 0 for no buffering, if you set it to greater than 1, it will start buffering from that buffer size.<\/p>\n<h4 id=\"fileAttributes\" class=\"post_h4\">File Attributes<\/h4>\n<p>Once a file is opened, you can get the values of different attributes like the name of the file, the mode of the file, and whether the file is opened or closed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">file_attributes = open(\"test.txt\", \"wb\")\r\nprint(\"Name of the file: \", file_attributes.name)\r\nprint(\"Closed or not : \", file_attributes.closed)\r\nprint(\"Opening mode : \", file_attributes.mode)<\/pre>\n<p>When you execute the above snippet, it produces the following output.<\/p>\n<figure id=\"attachment_622\" class=\"wp-caption aligncenter\" style=\"width: 334px;\" aria-describedby=\"caption-attachment-622\"><figcaption id=\"caption-attachment-622\" class=\"wp-caption-text\">\n<figure id=\"attachment_532\" aria-describedby=\"caption-attachment-532\" style=\"width: 373px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-532 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_fileIO.png\" alt=\"File IO Operations in Python\" width=\"373\" height=\"116\" data-srcset=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_fileIO.png 373w, https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_fileIO.png 300w\" data-sizes=\"(max-width: 373px) 100vw, 373px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 373px; --smush-placeholder-aspect-ratio: 373\/116;\" \/><figcaption id=\"caption-attachment-532\" class=\"wp-caption-text\">File IO Operations in Python<\/figcaption><\/figure>\n<\/figcaption><\/figure>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/file_operations.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<h4 id=\"writeToFile\" class=\"post_h4\">Writing to a file<\/h4>\n<p>For writing content to a file, you first need to <code>open()<\/code> it in writing mode, any of <code>w<\/code>, <code>w+<\/code>. `wb`, <code>wb+<\/code>\u00a0these modes can be used to write content to a file.<\/p>\n<p>You can write either in binary format or normal text format. If you write in a binary format you need to convert the content to bytes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">file_write = open(\"test.txt\", \"wb\")\r\nfile_write.write(bytes(\"Python is a great language.\", 'UTF-8'))\r\nfile_write.close()<\/pre>\n<p>Without binary format,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">file_write = open(\"test.txt\", \"w\")\r\nfile_write.write(\"Python is a great language.\")\r\nfile_write.close()<\/pre>\n<p>Both the above snippets create a new file if the file does not exist and if the file exists, it overwrites the content.<\/p>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/file_operations.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<h4 id=\"readFromFile\" class=\"post_h4\">Reading from a file<\/h4>\n<p>For reading the content in a file, you need to <code>open()<\/code> the file, and then use the Python <code>read()<\/code> function to read the data.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Open a file\r\nfile_read = open(\"test.txt\", \"r+\")\r\ntext = file_read.read()\r\nprint(\"Read String is : \", text)\r\nfile_read.close()<\/pre>\n<p>You can open the file in any mode which supports reading a file.<\/p>\n<h4 id=\"appendToFile\" class=\"post_h4\">Appending content to a file<\/h4>\n<p>When You need to add some extra content to the existing data, you should open the file in append mode, and do the rest as you will do in write mode. It is similar to the write mode which I have shown in the above paragraphs, the main difference is the write mode overwrites the content but here the append mode adds to the existing content.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Open a file\r\nfo_append = open(\"test.txt\", \"a\")\r\nfo_append.write(\"\\nPython is a powerful language.\")\r\nfo_append.close()<\/pre>\n<p>So now if I run the above snippet, it adds an extra line to the original content.<\/p>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/file_operations.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<h4 id=\"deleteFile\" class=\"post_h4\">Delete a file<\/h4>\n<p>If you need to delete any file, then, in that case, you can use Python\u2019s built-in function <code>remove()<\/code>\u00a0to delete a file. However, you need to import the `os` module to do this and pass a file name you want to delete as an argument\u00a0to <code>remove()<\/code> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import os\r\n\r\n#delete a file\r\nos.remove(\"test.txt\")<\/pre>\n<p>That is how you can handle different file operations in Python. Below is the complete code snippet related to the file handling I\/O operations covered above.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">__author__ = 'Avinash'\r\nimport os\r\n\r\n# Different file attributes\r\nfile_attributes = open(\"test.txt\", \"wb\")\r\nprint(\"Name of the file: \", file_attributes.name)\r\nprint(\"Closed or not : \", file_attributes.closed)\r\nprint(\"Opening mode : \", file_attributes.mode)\r\nfile_attributes.close()\r\n\r\n# Write to a file in binary format\r\nfile_write_bytes = open(\"test.txt\", \"wb\")\r\nfile_write_bytes.write(bytes(\"Python is a great language.\", 'UTF-8'))\r\nfile_write_bytes.close()\r\n\r\n# Write to a file\r\nfile_write = open(\"test.txt\", \"w\")\r\nfile_write.write(\"Python is a great language.\")\r\nfile_write.close()\r\n\r\n# Reading from a file\r\nfile_read = open(\"test.txt\", \"r+\")\r\ntext = file_read.read()\r\nprint(\"Read String is : \", text)\r\nfile_read.close()\r\n\r\n# Append to a file\r\nfile_append = open(\"test.txt\", \"a\")\r\nfile_append .write(\"\\nPython is a powerful language.\")\r\nfile_append.close()\r\n\r\n# Delete a file\r\nos.remove(\"test.txt\")\r\n<\/pre>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/file_operations.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<p>Also please feel free to check other posts on <a href=\"\/category\/programs-on-lists\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python Lists<\/a>, <a href=\"\/category\/programs-on-tuples\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python Tuples<\/a>, or <a href=\"\/category\/gui-programs\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python GUI programming<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello guys, welcome back to programminginpython.com! Here in this post, I will show you how to perform different File I\/O operations like opening a file, reading content from the file, writing data to a file &hellip;<\/p>\n","protected":false},"author":1,"featured_media":281,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[69,70,71],"class_list":["post-54","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-files-handling","tag-file-handling","tag-read","tag-write"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to perform File IO operations in Python - Programming In Python<\/title>\n<meta name=\"description\" content=\"A program to perform file IO Operation in python, Operations like read, write, append can be performed on files using 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:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to perform File IO operations in Python - Programming In Python\" \/>\n<meta property=\"og:description\" content=\"A program to perform file IO Operation in python, Operations like read, write, append can be performed on files using Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Programming In Python\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/programminginpython\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-30T04:56:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-20T16:17:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_file_IO_fb.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"AVINASH NETHALA\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@python_pip\" \/>\n<meta name=\"twitter:site\" content=\"@python_pip\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"AVINASH NETHALA\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to perform File IO operations in Python - Programming In Python","description":"A program to perform file IO Operation in python, Operations like read, write, append can be performed on files using 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:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to perform File IO operations in Python - Programming In Python","og_description":"A program to perform file IO Operation in python, Operations like read, write, append can be performed on files using Python.","og_url":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/","og_site_name":"Programming In Python","article_publisher":"https:\/\/www.facebook.com\/programminginpython","article_published_time":"2023-03-30T04:56:55+00:00","article_modified_time":"2023-04-20T16:17:15+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_file_IO_fb.png","type":"image\/png"}],"author":"AVINASH NETHALA","twitter_card":"summary_large_image","twitter_creator":"@python_pip","twitter_site":"@python_pip","twitter_misc":{"Written by":"AVINASH NETHALA","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/#article","isPartOf":{"@id":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/"},"author":{"name":"AVINASH NETHALA","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/9a3c14fe46d422ebf783ee61de1e788c"},"headline":"How to perform File IO operations in Python","datePublished":"2023-03-30T04:56:55+00:00","dateModified":"2023-04-20T16:17:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/"},"wordCount":619,"commentCount":0,"publisher":{"@id":"https:\/\/www.programminginpython.com\/#organization"},"image":{"@id":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_file_IO_WP1.png","keywords":["file handling","read","write"],"articleSection":["Files Handling"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/","url":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/","name":"How to perform File IO operations in Python - Programming In Python","isPartOf":{"@id":"https:\/\/www.programminginpython.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/#primaryimage"},"image":{"@id":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_file_IO_WP1.png","datePublished":"2023-03-30T04:56:55+00:00","dateModified":"2023-04-20T16:17:15+00:00","description":"A program to perform file IO Operation in python, Operations like read, write, append can be performed on files using Python.","breadcrumb":{"@id":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/#primaryimage","url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_file_IO_WP1.png","contentUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_file_IO_WP1.png","width":1920,"height":400,"caption":"How to perform File I\/O operations in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.programminginpython.com\/how-to-perform-file-io-operations-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.programminginpython.com\/"},{"@type":"ListItem","position":2,"name":"How to perform File IO operations in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.programminginpython.com\/#website","url":"https:\/\/www.programminginpython.com\/","name":"Programming In Python","description":"All About Python","publisher":{"@id":"https:\/\/www.programminginpython.com\/#organization"},"alternateName":"pip","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.programminginpython.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.programminginpython.com\/#organization","name":"Programming In Python","alternateName":"PIP","url":"https:\/\/www.programminginpython.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/04\/pip_logo_500_500.png","contentUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/04\/pip_logo_500_500.png","width":500,"height":500,"caption":"Programming In Python"},"image":{"@id":"https:\/\/www.programminginpython.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/programminginpython","https:\/\/x.com\/python_pip","https:\/\/www.youtube.com\/programminginpython","https:\/\/github.com\/avinashn\/programminginpython.com"]},{"@type":"Person","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/9a3c14fe46d422ebf783ee61de1e788c","name":"AVINASH NETHALA","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ed52e7670d7db94820c7430d324103ccdecb16d86611d5b29064aa9ce25a958b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ed52e7670d7db94820c7430d324103ccdecb16d86611d5b29064aa9ce25a958b?s=96&d=mm&r=g","caption":"AVINASH NETHALA"},"sameAs":["https:\/\/www.programminginpython.com\/"],"url":"https:\/\/www.programminginpython.com\/author\/avinash\/"}]}},"jetpack_featured_media_url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_file_IO_WP1.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":4,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":533,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/54\/revisions\/533"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/media\/281"}],"wp:attachment":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}