{"id":488,"date":"2023-02-23T15:15:43","date_gmt":"2023-02-23T15:15:43","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=488"},"modified":"2023-02-23T15:26:37","modified_gmt":"2023-02-23T15:26:37","slug":"appending-to-a-file","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/appending-to-a-file\/","title":{"rendered":"Appending to a file"},"content":{"rendered":"\n<p>When you append data to a file it is added after any existing file content. <\/p>\n\n\n\n<p>After the last example the output file that the program created would contain:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is the first line.\nThe second line\nAnd the third line<\/code><\/pre>\n\n\n\n<p>Using the following code, you could append further lines onto the output file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lines_to_write = &#091;\"This is the forth line.\", \n                               \"The fifth line\",\n                               \"And the sixth line\"]\n\nwith open(\"data2.txt\", \"a\") as input_file:\n    for line in lines_to_write:\n        input_file.write(str(line) + \"\\n\")<\/code><\/pre>\n\n\n\n<p>The contents of data2.txt would now be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is the first line.\nThe second line\nAnd the third line\nThis is the forth line.\nThe fifth line\nAnd the sixth line<\/code><\/pre>\n\n\n\n<p>By appending you don&#8217;t overwrite the existing content.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p>Open up write.py and change it to read:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string_to_write = \"How are you doing?\"\nwith open(\"HelloWorld.txt\", \"at\") as file_handler:\n    file_handler.write(string_to_write + \"\\n\")\nprint(\"File appended\")<\/code><\/pre>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/reading-and-writing\/\" data-type=\"page\" data-id=\"496\">Next: Reading and writing<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you append data to a file it is added after any existing file content. After the last example the output file that the program created would contain: Using the following code, you could append further lines onto the output file. The contents of data2.txt would now be: By appending you don&#8217;t overwrite the existing&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/appending-to-a-file\/\">Continue reading <span class=\"screen-reader-text\">Appending to a file<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":20,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-488","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=488"}],"version-history":[{"count":5,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/488\/revisions"}],"predecessor-version":[{"id":498,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/488\/revisions\/498"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/20"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}