{"id":203,"date":"2019-02-08T22:31:02","date_gmt":"2019-02-08T22:31:02","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/python\/?p=203"},"modified":"2020-07-26T09:22:56","modified_gmt":"2020-07-26T09:22:56","slug":"18-python-files-io","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/","title":{"rendered":"Python &#8211; Files &#038; IO"},"content":{"rendered":"<p>This lesson covers reading and writing to files. However, we would first review reading from keyboard and writing to the screen.<\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Writing to Screen Reading from Keyboard<\/a><\/li>\n<li><a href=\"#t2\">The input and raw_input Functions<\/a><\/li>\n<li><a href=\"#t3\">Creating a File<\/a><\/li>\n<li><a href=\"#t4\">The open method<\/a><\/li>\n<li><a href=\"#t5\">Attributes of a File Object<\/a><\/li>\n<li><a href=\"#t6\">The close() method<\/a><\/li>\n<li><a href=\"#t7\">Reading From File<\/a><\/li>\n<li><a href=\"#t8\">Writing to File<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Writing to Screen Reading from Keyboard<\/strong><\/h4>\n<p>You already know how to write to the screen. You use the print function and specify the text to be written as parameter. For example<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Hello World\"<\/span>)\r\nname <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span>\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"My name is\"<\/span> <span style=\"color: #333333;\">+<\/span> name)\r\n<\/pre>\n<p>You can run the code above to see the output. I also recommend you review how to us the printf() function under <a href=\"https:\/\/www.kindsonthegenius.com\/python\/python-strings\" target=\"_blank\" rel=\"noopener\">Python Strings.<\/a><\/p>\n<p>Now, you you can also read input from the keyboard using<\/p>\n<ul>\n<li>input Function<\/li>\n<li>raw_input Statement<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong id=\"t2\">2. The input() and<span style=\"color: #ff0000;\"> raw_input()<\/span> Functions<\/strong><\/p>\n<p>Both functions are used to read user input from the keyboard.<\/p>\n<p>The raw_input() reads a single line of input form the keyboard and returns it as a string. (<span style=\"color: #ff0000;\">not available in Python 3)<\/span><\/p>\n<p>The input() function also reads on line from the keyboard. However it assumes that the input is a valid Python expression. So it evaluates it and returns it.<\/p>\n<p>Examples are given below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">name <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">input<\/span>(<span style=\"background-color: #fff0f0;\">\"Please enter your name: \"<\/span>)\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"You entered \"<\/span> <span style=\"color: #333333;\">+<\/span> name)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Creating a File<\/strong><\/h4>\n<p>Now we we would discuss how to read and write from files.\u00a0 To be able to to that,\u00a0 you will be using the file object. The file object provide the functions you need to read and write to files in Python. One of such functions if the open() function.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. The open() Function<\/strong><\/h4>\n<p>Before you can read or write to file, you must first open the file using the open() function. The open() function returns a file object. Therefore you must assign it to a file variable. For example<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">myfile <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">open<\/span>(<span style=\"background-color: #fff0f0;\">\"C:\/test.txt\", \"r\"<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The above code creates a file object myfile. Then what is returned from the open() function is assigned to it. The second parameter &#8220;r&#8221; is the file mode. It indicates the the file is open for read-only. There are other file modes as well as listed in the table below<\/p>\n<p>&nbsp;<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Sr.No.<\/th>\n<th>File Modes and brief description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>r<\/b><\/p>\n<p>Opens a file for reading only and places the pointer in the beginning of the file.\u00a0<span style=\"font-family: inherit; font-size: inherit;\">\u00a0<\/span><span style=\"font-family: inherit; font-size: inherit;\">This is the default file\u00a0 mode.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>rb<\/b><\/p>\n<p>Opens a file for reading only but in binary format. This is the default mode.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>r+<\/b><\/p>\n<p>Opens a file for both read and write.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>rb+<\/b><\/p>\n<p>Opens a file for both read and write in binary format.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><b>w<\/b><\/p>\n<p>Opens a file for writing only. It however overwrites the file if the file exists. But it the file does not exist, it creates a new file for writing.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><b>wb<\/b><\/p>\n<p>Opens a file for writing only in binary format. It however overwrites the file if the file exists. But it the file does not exist, it creates a new file for writing.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><b>w+<\/b><\/p>\n<p>Opens a file for both writing and reading. It however overwrites the file if the file with same name exists. But it the file does not exist, it creates a new file for writing.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">8<\/td>\n<td><b>wb+<\/b><\/p>\n<p>Opens a file for both writing and reading in binary format. It however overwrites the file if the file with same name exists. But it the file does not exist, it creates a new file for writing.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">9<\/td>\n<td><b>a<\/b><\/p>\n<p>Opens a file for append. The file pointer is placed at the end of the file if the file with same name exists. So whatever is written is appended to the end of the file. But it the file does not exist, it creates a new file for writing.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">10<\/td>\n<td><b>ab<\/b><\/p>\n<p>Opens a file for appending in binary format. The file pointer is placed at the end of the file if the file with same name exists. So whatever is written is appended to the end of the file. But it the file does not exist, it creates a new file for writing.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">11<\/td>\n<td><b>a+<\/b><\/p>\n<p>Opens a file for both appending and reading. The file pointer is placed at the end of the file if the file with same name exists. So whatever is written is appended to the end of the file. But it the file does not exist, it creates a new file for writing.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">12<\/td>\n<td><b>ab+<\/b><\/p>\n<p>Opens a file for both appending and reading in binary format. The file pointer is placed at the end of the file if the file with same name exists. So whatever is written is appended to the end of the file. But it the file does not exist, it creates a new file for both reading and writing.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">\u00a05. Attributes of the file Object<\/strong><\/h4>\n<p>So if you have opened a file, what can you do with it? Now, the file object provides a number of attributes that give you information about the file. These attributes are tabulated below<\/p>\n<p>&nbsp;<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>SN<\/th>\n<th>Attribute and brief description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>file.closed<\/b><\/p>\n<p>Checks if the file is closed. Returns true if the file is closes. Returns false if otherwise<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>file.mode<\/b><\/p>\n<p>Returns file\u00a0 access mode with which file was opened with<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>file.name<\/b><\/p>\n<p>Returns name of the file on disk.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>The code below illustrates how to open a file and get information about the file.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># open a file called Tutorial.txt<\/span>\r\nmyfile <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">open<\/span>(<span style=\"background-color: #fff0f0;\">\"D:\/data\/Tutorial.txt\"<\/span>, <span style=\"background-color: #fff0f0;\">\"rb\"<\/span>)\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Name of file: \"<\/span> <span style=\"color: #333333;\">+<\/span> myfile<span style=\"color: #333333;\">.<\/span>name)\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Is it closed: \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">str<\/span>(myfile<span style=\"color: #333333;\">.<\/span>closed))\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"The file mode: \"<\/span> <span style=\"color: #333333;\">+<\/span> myfile<span style=\"color: #333333;\">.<\/span>mode)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If you run the code , then you will have the output give below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Name of file: D:<span style=\"color: #333333;\">\/<\/span>data<span style=\"color: #333333;\">\/<\/span>Tutorial<span style=\"color: #333333;\">.<\/span>txt\r\nIs it closed: <span style=\"color: #008800; font-weight: bold;\">False<\/span>\r\nThe file mode: rb\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong>6. The <em>close()<\/em> Method<\/strong><\/h4>\n<p>Just as you can guess by now, if you open a file, you should be able to close it. You can do this using the <em>close()<\/em> method.\u00a0 However, in addition to closing the file, the close() method also flushes any unwritten information before closing the file. It&#8217;s good to close a file after using it by explicitly calling the close() method. However, if you forget to close a file, Python would automatically close it if it goes out of scope.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t7\">7. Reading the Content of\u00a0 File<\/strong><\/h4>\n<p>No that you have opened a file, the next thing to do is to read the content of the file. Probably display the content to the output.<\/p>\n<p>First, you need to open the file in read mode. Then you call the <em><strong>read(n)<\/strong><\/em> method of the file object. The read method takes an optional parameter which is the number of characters to read from the file. If n is not specified, then the entire file is read.<\/p>\n<p>For example, the file Tutorial.txt contains list of names. The program below opens the file and reads the content of the file, then prints it out to the output.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># open a file called Tutorial.txt<\/span>\r\nmyfile <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">open<\/span>(<span style=\"background-color: #fff0f0;\">\"D:\/data\/Tutorial.txt\"<\/span>, <span style=\"background-color: #fff0f0;\">\"r\"<\/span>)\r\n\r\nname <span style=\"color: #333333;\">=<\/span> myfile<span style=\"color: #333333;\">.<\/span>read()\r\n\r\n<span style=\"color: #007020;\">print<\/span>(name)\r\n\r\nmyfile<span style=\"color: #333333;\">.<\/span>close()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The above code reads the content of the file and assigns it to a variable name. So name would now hold all the content of the file. So when we call print(name), the entire content of the file is printed out a shown below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Kindson Munonye\r\nJackie Ugwueke\r\nSolace Okeke\r\nSaffron Imolode\r\n<\/pre>\n<p>The table below gives a summary of all the methods you can use to read data from a file.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>SN<\/th>\n<th>Method and decription<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>read()<\/b><\/p>\n<p>Read the entire content of a file<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>read(n)<\/b><\/p>\n<p>Reads only n characters from a file<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>readline()\u00a0<\/b><\/p>\n<p>Reads a line of text from the file<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">\u00a04<\/td>\n<td><b>readlines()<\/b><\/p>\n<p>Reads all the content of a file into a list line by line<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t8\">8. Writing to File<\/strong><\/h4>\n<p>In\u00a0 addition to reading content of a\u00a0 file, you can also write data into a file. To do that we use the write method. You use the write method to write a string into a file. However, the line method does not add a newline character to the end of the file.<\/p>\n<p>The code below open a file in write mode. Then it writes three items into the file.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># open a file called Tutorial2.txt for writing<\/span>\r\nmyfile <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">open<\/span>(<span style=\"background-color: #fff0f0;\">\"D:\/data\/Tutorial2.txt\"<\/span>, <span style=\"background-color: #fff0f0;\">\"w\"<\/span>)\r\n\r\nmyfile<span style=\"color: #333333;\">.<\/span>write(<span style=\"background-color: #fff0f0;\">\"Python Programming Tutorials <\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0;\">\\n<\/span><span style=\"background-color: #fff0f0;\">\"<\/span>)\r\nmyfile<span style=\"color: #333333;\">.<\/span>write(<span style=\"background-color: #fff0f0;\">\"Coding Challenge <\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0;\">\\n<\/span><span style=\"background-color: #fff0f0;\">\"<\/span>)\r\nmyfile<span style=\"color: #333333;\">.<\/span>write(<span style=\"background-color: #fff0f0;\">\"Java Programming Tutorial\"<\/span>)\r\n\r\n\r\nmyfile<span style=\"color: #333333;\">.<\/span>close()\r\n<\/pre>\n<p>If you execute this code it creates a file named Tutorials2.txt in the location D:\/data and writes the the specified strings into the file.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This lesson covers reading and writing to files. However, we would first review reading from keyboard and writing to the screen. We would cover the &hellip; <\/p>\n","protected":false},"author":395,"featured_media":207,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[18],"class_list":["post-203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-reading-and-writing-to-files-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python - Files &amp; IO - Python Tutorials<\/title>\n<meta name=\"description\" content=\"You will learn about reading and writing to files in Python. How to ceate a new file. How to read data from file and how to write data to file\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python - Files &amp; IO - Python Tutorials\" \/>\n<meta property=\"og:description\" content=\"You will learn about reading and writing to files in Python. How to ceate a new file. How to read data from file and how to write data to file\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-08T22:31:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-07-26T09:22:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Files-and-IO-in-Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1005\" \/>\n\t<meta property=\"og:image:height\" content=\"544\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Python &#8211; Files &#038; IO\",\"datePublished\":\"2019-02-08T22:31:02+00:00\",\"dateModified\":\"2020-07-26T09:22:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/\"},\"wordCount\":1295,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Files-and-IO-in-Python.jpg\",\"keywords\":[\"Reading and Writing to Files in Python.\"],\"articleSection\":[\"Python Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/\",\"name\":\"Python - Files & IO - Python Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Files-and-IO-in-Python.jpg\",\"datePublished\":\"2019-02-08T22:31:02+00:00\",\"dateModified\":\"2020-07-26T09:22:56+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"You will learn about reading and writing to files in Python. How to ceate a new file. How to read data from file and how to write data to file\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Files-and-IO-in-Python.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Files-and-IO-in-Python.jpg\",\"width\":1005,\"height\":544,\"caption\":\"Files and IO\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/18-python-files-io\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python &#8211; Files &#038; IO\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\",\"name\":\"Python Tutorials\",\"description\":\"Python Tutorial for Programming and Data Science\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python - Files & IO - Python Tutorials","description":"You will learn about reading and writing to files in Python. How to ceate a new file. How to read data from file and how to write data to file","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/","og_locale":"en_US","og_type":"article","og_title":"Python - Files & IO - Python Tutorials","og_description":"You will learn about reading and writing to files in Python. How to ceate a new file. How to read data from file and how to write data to file","og_url":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/","og_site_name":"Python Tutorials","article_published_time":"2019-02-08T22:31:02+00:00","article_modified_time":"2020-07-26T09:22:56+00:00","og_image":[{"width":1005,"height":544,"url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Files-and-IO-in-Python.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Python &#8211; Files &#038; IO","datePublished":"2019-02-08T22:31:02+00:00","dateModified":"2020-07-26T09:22:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/"},"wordCount":1295,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Files-and-IO-in-Python.jpg","keywords":["Reading and Writing to Files in Python."],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/","url":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/","name":"Python - Files & IO - Python Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Files-and-IO-in-Python.jpg","datePublished":"2019-02-08T22:31:02+00:00","dateModified":"2020-07-26T09:22:56+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"You will learn about reading and writing to files in Python. How to ceate a new file. How to read data from file and how to write data to file","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Files-and-IO-in-Python.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Files-and-IO-in-Python.jpg","width":1005,"height":544,"caption":"Files and IO"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/python\/18-python-files-io\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/python\/"},{"@type":"ListItem","position":2,"name":"Python &#8211; Files &#038; IO"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/python\/#website","url":"https:\/\/www.kindsonthegenius.com\/python\/","name":"Python Tutorials","description":"Python Tutorial for Programming and Data Science","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/python\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":4,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":381,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/203\/revisions\/381"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media\/207"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}