{"id":600,"date":"2021-03-18T12:33:46","date_gmt":"2021-03-18T12:33:46","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=600"},"modified":"2021-06-24T09:23:36","modified_gmt":"2021-06-24T08:23:36","slug":"writing-data-to-a-text-file","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/higher\/writing-data-to-a-text-file\/","title":{"rendered":"Writing data to a text file"},"content":{"rendered":"<p>The file.write() command only takes a single parameter &#8211; this is best achieved using an f-string.<\/p>\n<p>End-of-line characters &#8220;\\&#8221; should also be written.<\/p>\n<h1>Writing lines of text<\/h1>\n<p>Opening a file with &#8220;w&#8221; parameter will create a new file, or overwrite any existing file.<\/p>\n<p>Opening a file with &#8220;a&#8221; parameter will create a new file, or append to any existing file.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nOUT_FILE= 'results.txt'\r\n \r\ndef writeDataToFile(data):\r\n    f = open(OUT_FILE, 'a')\r\n    f.write(f&quot;{data}\\n&quot;)\r\n    f.close()\r\n    print(&quot;data written&quot;)\r\n\r\n#MAIN PROGRAM\r\nwriteDataToFile(&quot;fred&quot;)\r\nwriteDataToFile(&quot;alice&quot;)\r\nwriteDataToFile(&quot;sue&quot;)\r\n<\/pre>\n<h1>Writing arrays<\/h1>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nOUT_FILE= 'results.txt'\r\n\r\ndef writeDataToFile(names, marks):\r\n    with open(OUT_FILE, 'w') as f:\r\n        for student in range(len(marks)):\r\n            f.write(f&quot;{names&#x5B;student]},{marks&#x5B;student]}\\n&quot;)\r\n    print(f&quot;Data written to {OUT_FILE}&quot;)\r\n\r\n#MAIN PROGRAM\r\n#\r\nwriteDataToFile(names, marks)\r\n#<\/pre>\n<h1>Writing records<\/h1>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nOUT_FILE= 'results.txt'\r\n\r\ndef writeDataToFile(students):\r\n    with open(OUT_FILE, 'w') as f:\r\n        for student in students:\r\n            f.write(f&quot;{student.name},{student.mark}\\n&quot;)\r\n    print(f&quot;Data written to {OUT_FILE}&quot;)\r\n\r\n#MAIN PROGRAM\r\n#\r\nwriteDataToFile(students)\r\n#<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The file.write() command only takes a single parameter &#8211; this is best achieved using an f-string. End-of-line characters &#8220;\\&#8221; should also be written. Writing lines of text Opening a file with &#8220;w&#8221; parameter will create a new file, or overwrite any existing file. Opening a file with &#8220;a&#8221; parameter will create a new file, or append to any existing file. # OUT_FILE= &#8216;results.txt&#8217; def writeDataToFile(data): f = open(OUT_FILE, &#8216;a&#8217;) f.write(f&quot;{data}\\n&quot;)<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/higher\/writing-data-to-a-text-file\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":330,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-600","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=600"}],"version-history":[{"count":6,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/600\/revisions"}],"predecessor-version":[{"id":607,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/600\/revisions\/607"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/330"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}