{"id":482,"date":"2023-02-23T13:34:12","date_gmt":"2023-02-23T13:34:12","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=482"},"modified":"2023-02-23T15:19:21","modified_gmt":"2023-02-23T15:19:21","slug":"writing-to-a-file","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/writing-to-a-file\/","title":{"rendered":"Writing to a file"},"content":{"rendered":"\n<p>To write a line to a file you use the write() function on the file handler.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lines_to_write = &#091;\"This is the first line.\", \n                          \"The second line\",\n                          \"And the third line\"]\n\nwith open(\"data2.txt\", \"w\") as input_file:\n    for line in lines_to_write:\n        input_file.write(str(line) + \"\\n\")<\/code><\/pre>\n\n\n\n<p>Notice the special \\n character we add to each line. This is a hidden character that signifies a new line. Remember that writing to a file removes the current contents of the file and overwrites with the new data.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p>Try the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string_to_write = \"Hello World!\"\nwith open(\"HelloWorld.txt\", \"wt\") as file_handler:\n    file_handler.write(string_to_write + \"\\n\")\nprint(\"File written\")<\/code><\/pre>\n\n\n\n<p>The file should be made within the same directory as the Python script. Open up the HellowWorld.txt file and check the contents.<\/p>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/appending-to-a-file\/\" data-type=\"page\" data-id=\"488\">Next: Appending to a file<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To write a line to a file you use the write() function on the file handler. Notice the special \\n character we add to each line. This is a hidden character that signifies a new line. Remember that writing to a file removes the current contents of the file and overwrites with the new data.&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/writing-to-a-file\/\">Continue reading <span class=\"screen-reader-text\">Writing to a file<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":20,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-482","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/482","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=482"}],"version-history":[{"count":3,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/482\/revisions"}],"predecessor-version":[{"id":492,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/482\/revisions\/492"}],"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=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}