{"id":8959,"date":"2020-03-23T23:45:56","date_gmt":"2020-03-23T18:15:56","guid":{"rendered":"https:\/\/java2blog.com\/?p=8959"},"modified":"2023-11-25T10:21:43","modified_gmt":"2023-11-25T04:51:43","slug":"python-write-text-file","status":"publish","type":"post","link":"https:\/\/java2blog.com\/python-write-text-file\/","title":{"rendered":"Python write to text file"},"content":{"rendered":"<p>In this post, we will see how to write to a text file.<\/p>\n<p>There are multiple modes in which you can write to <code>text<\/code> file.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Create_txt_file8216x8217\">Create txt file(&#8216;x&#8217;)<\/a><\/li><li><a href=\"#Write_to_txt_file8216w8217\">Write to txt file(&#8216;w&#8217;)<\/a><\/li><li><a href=\"#Append_to_txt_file8216a8217\">Append to txt file(&#8216;a&#8217;)<\/a><\/li><\/ul><\/div>\n\n<h2><span id=\"Create_txt_file8216x8217\">Create txt file(&#8216;x&#8217;)<\/span><\/h2>\n<p>It will open the file in <code>'X'<\/code> mode which will create new file and return error in case file already exists.<\/p>\n<pre class=\"lang:python decode:1 \">\nfruitsStr = ' Apple \\n Orange \\n Banana \\n Pineapple'\nwith open('fruits.txt','x') as file:\n    file.write(fruitsStr)\n<\/pre>\n<p>Here is content of fruits.txt.<\/p>\n<div style=\"background-color:black;color:white;padding:10px;\">\n$ cat fruits.txt<br \/>\n Apple<br \/>\n Orange<br \/>\n Banana<br \/>\n Pineapple\n<\/div>\n<h2><span id=\"Write_to_txt_file8216w8217\">Write to txt file(&#8216;w&#8217;)<\/span><\/h2>\n<p>It will open the file in <code>'w'<\/code> mode which will write to file and will override the content of <code>fruits.txt<\/code><\/p>\n<pre class=\"lang:python decode:1 \">\nfruitsStr = ' Mango \\n Grapes'\nwith open('fruits.txt','w') as file:\n    file.write(fruitsStr)\n<\/pre>\n<div style=\"background-color:black;color:white;padding:10px;\">\n$ cat fruits.txt<br \/>\n Mango<br \/>\n Grapes\n<\/div>\n<p>As you can see, it has overridden content of file which we have read in <code>'w'<\/code> mode.<\/p>\n<h2><span id=\"Append_to_txt_file8216a8217\">Append to txt file(&#8216;a&#8217;)<\/span><\/h2>\n<p>It will open the file in &#8216;a&#8217; mode which will append to the file.<\/p>\n<pre class=\"lang:python decode:1 \">\nfruitsStr = '\\n Lichy \\n Pomegranate'\nwith open('fruits.txt','a') as file:\n    file.write(fruitsStr)\n<\/pre>\n<div style=\"background-color:black;color:white;padding:10px;\">\n$ cat fruits.txt<br \/>\n Mango<br \/>\n Grapes<br \/>\n Lichy<br \/>\n Pomegranate\n<\/div>\n<p>As you can see Lichy and Pomegranate were appended to the content of <code>fruits.txt<\/code>.<\/p>\n<p>That&#8217;s all about Python write to text file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsCreate txt file(&#8216;x&#8217;)Write to txt file(&#8216;w&#8217;)Append to txt file(&#8216;a&#8217;) In this post, we will see how to write to a text file. There are multiple modes in which you can write to text file. Create txt file(&#8216;x&#8217;) It will open the file in &#8216;X&#8217; mode which will create new file and return error [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145,185],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/8959"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=8959"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/8959\/revisions"}],"predecessor-version":[{"id":25735,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/8959\/revisions\/25735"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=8959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=8959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=8959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}