{"id":6761,"date":"2023-03-07T20:05:01","date_gmt":"2023-03-07T12:05:01","guid":{"rendered":"https:\/\/tommykwan.com\/blog\/?p=6761"},"modified":"2023-03-07T20:05:01","modified_gmt":"2023-03-07T12:05:01","slug":"python-data-to-text-txt","status":"publish","type":"post","link":"https:\/\/tommykwan.com\/blog\/it\/python-data-to-text-txt\/","title":{"rendered":"Python : Data to Text (.txt)"},"content":{"rendered":"<p>Ref:<a href=\"https:\/\/www.w3schools.com\/python\/python_file_write.asp\"> https:\/\/www.w3schools.com\/python\/python_file_write.asp<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>To write to an existing file, you must add a parameter to the\u00a0<code class=\"w3-codespan\">open()<\/code>\u00a0function:<\/p>\n<p><code class=\"w3-codespan\">\"a\"<\/code>\u00a0&#8211; Append &#8211; will append to the end of the file<\/p>\n<p><code class=\"w3-codespan\">\"w\"<\/code>\u00a0&#8211; Write &#8211; will overwrite any existing content<\/p>\n<p>&nbsp;<\/p>\n<p>f =\u00a0<span class=\"pythonkeywordcolor\">open<\/span>(<span class=\"pythonstringcolor\">&#8220;demofile2.txt&#8221;<\/span>,\u00a0<span class=\"pythonstringcolor\">&#8220;<strong>a<\/strong>&#8220;<\/span>)<br \/>\nf.write(<span class=\"pythonstringcolor\">&#8220;Now the file has more content!&#8221;<\/span>)<br \/>\nf.close()<\/p>\n<p>&nbsp;<\/p>\n<p>f =\u00a0<span class=\"pythonkeywordcolor\">open<\/span>(<span class=\"pythonstringcolor\">&#8220;demofile3.txt&#8221;<\/span>,\u00a0<span class=\"pythonstringcolor\">&#8220;<strong>w<\/strong>&#8220;<\/span>)<br \/>\nf.write(<span class=\"pythonstringcolor\">&#8220;Woops! I have deleted the content!&#8221;<\/span>)<br \/>\nf.close()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ref: https:\/\/www.w3schools.com\/python\/python_file_write.asp &nbsp; To write to an existing file, you must add a parameter to the\u00a0open()\u00a0function: &#8220;a&#8221;\u00a0&#8211; Append &#8211; will append to the end of the file &#8220;w&#8221;\u00a0&#8211; Write &#8211; will overwrite any existing content &nbsp; f =\u00a0open(&#8220;demofile2.txt&#8221;,\u00a0&#8220;a&#8220;) f.write(&#8220;Now the file has more content!&#8221;) f.close() &nbsp; f =\u00a0open(&#8220;demofile3.txt&#8221;,\u00a0&#8220;w&#8220;) f.write(&#8220;Woops! I have deleted the content!&#8221;) f.close()<a class=\"read-more \" href=\"https:\/\/tommykwan.com\/blog\/it\/python-data-to-text-txt\/\" title=\"Read More\"> <span class=\"button default\">Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,28],"tags":[],"class_list":["post-6761","post","type-post","status-publish","format-standard","hentry","category-it","category-python"],"_links":{"self":[{"href":"https:\/\/tommykwan.com\/blog\/wp-json\/wp\/v2\/posts\/6761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tommykwan.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tommykwan.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tommykwan.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tommykwan.com\/blog\/wp-json\/wp\/v2\/comments?post=6761"}],"version-history":[{"count":1,"href":"https:\/\/tommykwan.com\/blog\/wp-json\/wp\/v2\/posts\/6761\/revisions"}],"predecessor-version":[{"id":6762,"href":"https:\/\/tommykwan.com\/blog\/wp-json\/wp\/v2\/posts\/6761\/revisions\/6762"}],"wp:attachment":[{"href":"https:\/\/tommykwan.com\/blog\/wp-json\/wp\/v2\/media?parent=6761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tommykwan.com\/blog\/wp-json\/wp\/v2\/categories?post=6761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tommykwan.com\/blog\/wp-json\/wp\/v2\/tags?post=6761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}