{"id":19608,"date":"2020-10-22T22:09:34","date_gmt":"2020-10-23T05:09:34","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19608"},"modified":"2023-12-01T04:03:22","modified_gmt":"2023-12-01T12:03:22","slug":"python-write-to-file","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/","title":{"rendered":"Python Write to File: A Guide"},"content":{"rendered":"\n<p><em>he open() function writes contents to an existing file. You must use the &#8220;w&#8221;, &#8220;a&#8221;, &#8220;r+&#8221;, &#8220;a+&#8217;, or &#8220;x&#8221; file modes to write text to a file. The most common file modes are &#8220;w&#8221; and &#8220;a&#8221;. These modes write data to a file and append data to a file, respectively.<\/em><\/p>\n\n\n\n<p>Do you need to make a change to a file? Python has you covered. You can write data to a new file or modify existing data in a file using Python&#8217;s built-in I\/O functions.<\/p>\n\n\n\n<p>In this guide, we\u2019re going to discuss how to write to a file in Python. We\u2019ll walk through an example to illustrate how to write to a file. Let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Write to a File in Python<\/h2>\n\n\n\n<p>You can write to a file in Python using the <a href=\"https:\/\/careerkarma.com\/blog\/python-read-file\/\">open() function<\/a>. You must specify either &#8220;w&#8221; or &#8220;a&#8221; as a parameter to write to a file. &#8220;w&#8221; overwrites the existing content of a file. &#8220;a&#8221; appends content to a file.<\/p>\n\n\n\n<p>In Python, you can write to both text and binary files. For this tutorial, we&#8217;re going to focus on text files. These are files without any specific encoding, and so they can be opened using a text editor. Text files include .csv files, .txt files, and .md files.<\/p>\n\n\n\n<p>There\u2019s no need to import any external library to write to a file in Python. The Python programming language has a built-in suite of tools you can use to write to a file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Open a File for Writing<\/h2>\n\n\n\n<p>Before we can write to a file, we need to learn how to open one. Let\u2019s say that we want to write a list of ingredients for a scone into a list. We\u2019d start by opening up a file called <em>scone.txt<\/em> like this:<\/p>\n\n\n\n<p><em>scone_file = open(\u201cscone.txt\u201d, \u201cw\u201d)<\/em><\/p>\n\n\n\n<p>We\u2019ve opened our scone.txt file using the <em>w<\/em> mode. This means that we can write to the file with which we are working. There are a couple of different modes that you may want to use. The ones we need for writing are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>w: This mode allows you to write to a file. It erases the contents of a file and creates a new one.<\/li><li>a: This mode appends information to the end of a file.<\/li><li>r+: This mode allows you to read information from and write data to a file.<\/li><li>a+: This mode allows you to add information to the end of a file and read the file.<\/li><li>x: Creates a file if one does not already exist to which we can add data.<\/li><\/ul>\n\n\n\n<p>When you open a file in Python, you need to close it afterward. Otherwise, Python will automatically close and delete the file. The best way to do close the file you are working with automatically is by using a with statement:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>with open(&quot;scone.txt&quot;, &quot;w&quot;) as file:\n\t\/\/ Work with the file\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Python Write to a File<\/h2>\n\n\n\n<p>We want to start adding the ingredients for our scone to our file. To do so, we can use the <em>write()<\/em> function. This adds the characters you specify to the end of a file.<\/p>\n\n\n\n<p>When you create a new file object, a new file will be created if one does not already exist. We\u2019re going to use the \u201cw\u201d mode to write to our scones.txt file because it does not currently include any information.<\/p>\n\n\n\n<p>Let\u2019s add three ingredients to our scones.txt file:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>with open(&quot;scone.txt&quot;, &quot;w&quot;) as file:\n\tfile.write(&quot;350g self-raising flour\\n&quot;)\n\tfile.write(&quot;1 tsp baking powder\\n&quot;)\n\tfile.write(&quot;85g butter\\n&quot;)\n<\/pre><\/div>\n\n\n\n<p>When this code executes, three lines of text are added to scone.txt. We\u2019ve used \u201c\\n\u201d characters at the end of each line to indicate that we want <a href=\"https:\/\/careerkarma.com\/blog\/python-print-without-new-line\/\">new lines to appear in our text<\/a>. These are called newline characters and print a line to the file.<\/p>\n\n\n\n<p>If we don\u2019t specify these characters, our text would all be written on the same line.<\/p>\n\n\n\n<p>Let\u2019s open our scones.txt file:<\/p>\n\n\n\n<p>350g self-raising flour<\/p>\n\n\n\n<p>1 tsp baking powder<\/p>\n\n\n\n<p>85g butter<\/p>\n\n\n\n<p>Our file has three lines of text! Now, we\u2019ve got a few more ingredients to add. To add them, we\u2019re going to open our file in <em>append<\/em> mode.<\/p>\n\n\n\n<p>If we opened up our file in <em>write<\/em> mode, a new file would be created. We would lose the ingredients we already added to the file. Let\u2019s open the file:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>with open(&quot;scone.txt&quot;, &quot;a&quot;) as file:\n\tfile.write(&quot;3 tbsp caster sugar\\n&quot;)\n\tfile.write(&quot;175ml milk\\n&quot;)\n\tfile.write(&quot;1 tsp vanilla extract\\n&quot;)\n<\/pre><\/div>\n\n\n\n<p>This code adds three files of text to scones.txt:<\/p>\n\n\n\n<p>350g self-raising flour<\/p>\n\n\n\n<p>1 tsp baking powder<\/p>\n\n\n\n<p>85g butter<\/p>\n\n\n\n<p>3 tbsp caster sugar<\/p>\n\n\n\n<p>175ml milk<\/p>\n\n\n\n<p>1 tsp vanilla extract<\/p>\n\n\n\n<p>We did it! We\u2019ve added text to a text file in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Write to an Existing File<\/h2>\n\n\n\n<p>You can write to an existing file using the open() function and the &#8220;a&#8221; parameter. You can only write to the end of a file.<\/p>\n\n\n\n<p>This means that there\u2019s no way for you to edit an existing file using only the built-in Python file operations. There is a workaround that we can create. This involves reading a file into a list and then manipulating the contents of the list.<\/p>\n\n\n\n<p>There is no limit to the number of characters you can read and write to files in Python. You can only write string data to a file. You cannot write integers, floating-points, or other data types to a file.<\/p>\n\n\n\n<p>Let\u2019s say that we want to add \u201cScone Ingredients\u201d to the top of our ingredients list. We could do this by reading our ingredients into a list and then inserting our new line of text at the start using <em>insert().<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Prepare a List to Write to a File<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/careerkarma.com\/blog\/python-insert\/\"><em>insert()<\/em><\/a> <a href=\"https:\/\/careerkarma.com\/blog\/python-insert\/\">function<\/a> accepts two parameters. You must first specify the index position at which you want to insert an item. Then, you must specify the value you want to insert into the list. Consider this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>with open(&quot;scones.txt&quot;, &quot;r&quot;) as file:\n\tscone_file = file.readlines()\n\nscone_file.insert(0, &quot;Scone Ingredients\\n&quot;)\n\nwith open(&quot;scones.txt&quot;, &quot;w&quot;) as scones:\n\tcontents = &quot;&quot;.join(scone_file)\n\tscones.write(contents)\n<\/pre><\/div>\n\n\n\n<p>We\u2019ve started by opening our file in \u201cread\u201d mode. We use the <em>readlines()<\/em> function to retrieve all the contents of our existing file. This returns a list of the lines in our file. We use the insert() function to add \u201cScone Ingredients\\n\u201d to the index position 0 in our list of file lines.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Python Write to Text File<\/h3>\n\n\n\n<p>Once we\u2019ve added the text we want to add, we write all the contents to the file. We use the <a href=\"https:\/\/careerkarma.com\/blog\/python-join\/\"><em>.join()<\/em><\/a> <a href=\"https:\/\/careerkarma.com\/blog\/python-join\/\">function<\/a> to convert our list of file lines to a string. We then write that value to the file.<\/p>\n\n\n\n<p>Let\u2019s open up our scone.txt file data:<\/p>\n\n\n\n<p>Scone Ingredients<\/p>\n\n\n\n<p>350g self-raising flour<\/p>\n\n\n\n<p>1 tsp baking powder<\/p>\n\n\n\n<p>85g butter<\/p>\n\n\n\n<p>3 tbsp caster sugar<\/p>\n\n\n\n<p>175ml milk<\/p>\n\n\n\n<p>1 tsp vanilla extract<\/p>\n\n\n\n<p>Our list now starts with \u201cScone Ingredients\u201d.<\/p>\n\n\n\n<p>Similarly, we can edit the contents of our list of file lines. Let\u2019s say that we want to increase the quantity of butter in our recipe to 95g. We could do so using this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>with open(&quot;scones.txt&quot;, &quot;r&quot;) as file:\n\tscone_file = file.readlines()\n\nscone_file[3] = &quot;95g butter\\n&quot;\n\nwith open(&quot;scones.txt&quot;, &quot;w&quot;) as scones:\n\tcontents = &quot;&quot;.join(scone_file)\n\tscones.write(contents)\n<\/pre><\/div>\n\n\n\n<p>We have used square bracket notation to change the value of the item at index position 3 in our list of ingredients. This item corresponds to our \u201cbutter\u201d ingredient.<\/p>\n\n\n\n<p>We set the value at this position in the list to \u201c95g\u201d of butter. We then write the revised contents of the file back to the file in write mode. Our code returns:<\/p>\n\n\n\n<p>Scone Ingredients<\/p>\n\n\n\n<p>350g self-raising flour<\/p>\n\n\n\n<p>1 tsp baking powder<\/p>\n\n\n\n<p>95g butter<\/p>\n\n\n\n<p>3 tbsp caster sugar<\/p>\n\n\n\n<p>175ml milk<\/p>\n\n\n\n<p>1 tsp vanilla extract<\/p>\n\n\n\n<p>Our recipe now calls for \u201c95g butter\u201d instead of \u201c85g butter\u201d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>You can read data from and write data to a file using the open() method. The two main modes that you\u2019ll use to write to a file are \u201ca\u201d and \u201cw\u201d, which stand for \u201cappend\u201d and \u201cwrite\u201d, respectively.<\/p>\n\n\n\n<p>To learn more about Python, read our complete guide on <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-python\/\">how to code in Python<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"he open() function writes contents to an existing file. You must use the \"w\", \"a\", \"r+\", \"a+', or \"x\" file modes to write text to a file. The most common file modes are \"w\" and \"a\". These modes write data to a file and append data to a file, respectively. Do you need to make&hellip;","protected":false},"author":240,"featured_media":5637,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-19608","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Write to File: A Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"Python allows you to read and write to files. On Career Karma, learn how to open a file for writing, write data to a file, and edit data in a 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:\/\/careerkarma.com\/blog\/python-write-to-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Write to File: A Guide\" \/>\n<meta property=\"og:description\" content=\"Python allows you to read and write to files. On Career Karma, learn how to open a file for writing, write data to a file, and edit data in a file.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-23T05:09:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:03:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/08\/goran-ivos-iacpoKgpBAM-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"1000\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\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:\/\/careerkarma.com\/blog\/python-write-to-file\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python Write to File: A Guide\",\"datePublished\":\"2020-10-23T05:09:34+00:00\",\"dateModified\":\"2023-12-01T12:03:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/\"},\"wordCount\":1270,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/08\/goran-ivos-iacpoKgpBAM-unsplash.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/\",\"name\":\"Python Write to File: A Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/08\/goran-ivos-iacpoKgpBAM-unsplash.jpg\",\"datePublished\":\"2020-10-23T05:09:34+00:00\",\"dateModified\":\"2023-12-01T12:03:22+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"Python allows you to read and write to files. On Career Karma, learn how to open a file for writing, write data to a file, and edit data in a file.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/08\/goran-ivos-iacpoKgpBAM-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/08\/goran-ivos-iacpoKgpBAM-unsplash.jpg\",\"width\":1000,\"height\":1000},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Write to File: A Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Write to File: A Guide | Career Karma","description":"Python allows you to read and write to files. On Career Karma, learn how to open a file for writing, write data to a file, and edit data in a 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:\/\/careerkarma.com\/blog\/python-write-to-file\/","og_locale":"en_US","og_type":"article","og_title":"Python Write to File: A Guide","og_description":"Python allows you to read and write to files. On Career Karma, learn how to open a file for writing, write data to a file, and edit data in a file.","og_url":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-10-23T05:09:34+00:00","article_modified_time":"2023-12-01T12:03:22+00:00","og_image":[{"width":1000,"height":1000,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/08\/goran-ivos-iacpoKgpBAM-unsplash.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python Write to File: A Guide","datePublished":"2020-10-23T05:09:34+00:00","dateModified":"2023-12-01T12:03:22+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/"},"wordCount":1270,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/08\/goran-ivos-iacpoKgpBAM-unsplash.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-write-to-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/","url":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/","name":"Python Write to File: A Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/08\/goran-ivos-iacpoKgpBAM-unsplash.jpg","datePublished":"2020-10-23T05:09:34+00:00","dateModified":"2023-12-01T12:03:22+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"Python allows you to read and write to files. On Career Karma, learn how to open a file for writing, write data to a file, and edit data in a file.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-write-to-file\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/08\/goran-ivos-iacpoKgpBAM-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/08\/goran-ivos-iacpoKgpBAM-unsplash.jpg","width":1000,"height":1000},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-write-to-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python Write to File: A Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=19608"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19608\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/5637"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}