{"id":398,"date":"2020-08-26T15:24:12","date_gmt":"2020-08-26T15:24:12","guid":{"rendered":"https:\/\/aaidm.buas.nl\/?page_id=398"},"modified":"2020-08-26T15:37:26","modified_gmt":"2020-08-26T15:37:26","slug":"pr1-preparation-for-week-8-writing-a-file","status":"publish","type":"page","link":"https:\/\/aaidm.buas.nl\/?page_id=398","title":{"rendered":"PR1 &#8211; Preparation for Week 8 &#8211; Writing a file"},"content":{"rendered":"\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<figure class=\"wp-block-table\"><table><tbody><tr><td>uthor:<\/td><td>Carlos Santos<\/td><\/tr><tr><td>Learning Line:<\/td><td>Programming<\/td><\/tr><tr><td>Course:<\/td><td>PR1: Introduction to Programming<\/td><\/tr><tr><td>Week:<\/td><td>8<\/td><\/tr><tr><td>Competencies:<\/td><td>Students will be able to effectively define and use variables, programming flow control.<\/td><\/tr><tr><td>BoKS:<\/td><td>\u00ad 3bK2, The student understands the principles of data related software like Python, R, Scala and\/or Java and knows how to write (basic) scripts.<\/td><\/tr><tr><td>Learning Goals:<\/td><td>Able write text files<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Writing a Text File<\/h2>\n\n\n\n<p>Writing a file is very similar to reading a file, you open the file, you write on it, and then you close the file.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">f = open(&quot;testfile.txt&quot;,&quot;w&quot;)\nf.write(&quot;hi&quot;)\nf.close() <\/pre><\/div>\n\n\n\n<p>Open actually has many operational modes.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Character<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>'r'<\/code><\/td><td>open for reading (default)<\/td><\/tr><tr><td><code>'w'<\/code><\/td><td>open for writing, truncating the file first<\/td><\/tr><tr><td><code>'x'<\/code><\/td><td>open for exclusive creation, failing if the file already exists<\/td><\/tr><tr><td><code>'a'<\/code><\/td><td>open for writing, appending to the end of the file if it exists<\/td><\/tr><tr><td><code>'b'<\/code><\/td><td>binary mode<\/td><\/tr><tr><td><code>'t'<\/code><\/td><td>text mode (default)<\/td><\/tr><tr><td><code>'+'<\/code><\/td><td>open for updating (reading and writing)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>It is perfectly normal to do something link <em>open(&#8220;testfile.txt&#8221;,&#8221;rw+t&#8221;)<\/em>, meaning  please open a <span style=\"text-decoration: underline;\">text<\/span>(t) file for <span style=\"text-decoration: underline;\">read<\/span>(r) and <span style=\"text-decoration: underline;\">write<\/span>(w) operations, and do not delete the content (+) we will be updating it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Writing Methods<\/h2>\n\n\n\n<p>Once you\u2019ve opened up a file, you\u2019ll want to write its content. You can using different methods.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>What It Does<\/th><\/tr><\/thead><tbody><tr><td><code>.write(string)<\/code><\/td><td>This writes the string to the file.<\/td><\/tr><tr><td><code>.writelines(seq)<\/code><\/td><td>This writes the sequence to the file. No line endings are appended to each sequence item. It\u2019s up to you to add the appropriate line ending(s).<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>uthor: Carlos Santos Learning Line: Programming Course: PR1: Introduction to Programming Week: 8 Competencies: Students will be able to effectively define and use variables, programming flow control. BoKS: \u00ad 3bK2, The student understands the principles of data related software like Python, R, Scala and\/or Java and knows how to write (basic) scripts. Learning Goals: Able [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"parent":99,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-398","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PR1 - Preparation for Week 8 - Writing a file - Applied Artificial Intelligence and Data Management<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/aaidm.buas.nl\/?page_id=398\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PR1 - Preparation for Week 8 - Writing a file - Applied Artificial Intelligence and Data Management\" \/>\n<meta property=\"og:description\" content=\"uthor: Carlos Santos Learning Line: Programming Course: PR1: Introduction to Programming Week: 8 Competencies: Students will be able to effectively define and use variables, programming flow control. BoKS: \u00ad 3bK2, The student understands the principles of data related software like Python, R, Scala and\/or Java and knows how to write (basic) scripts. Learning Goals: Able [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/aaidm.buas.nl\/?page_id=398\" \/>\n<meta property=\"og:site_name\" content=\"Applied Artificial Intelligence and Data Management\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-26T15:37:26+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/aaidm.buas.nl\/?page_id=398\",\"url\":\"https:\/\/aaidm.buas.nl\/?page_id=398\",\"name\":\"PR1 - Preparation for Week 8 - Writing a file - Applied Artificial Intelligence and Data Management\",\"isPartOf\":{\"@id\":\"https:\/\/aaidm.buas.nl\/#website\"},\"datePublished\":\"2020-08-26T15:24:12+00:00\",\"dateModified\":\"2020-08-26T15:37:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/aaidm.buas.nl\/?page_id=398#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/aaidm.buas.nl\/?page_id=398\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/aaidm.buas.nl\/?page_id=398#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/aaidm.buas.nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Year 1\",\"item\":\"https:\/\/aaidm.buas.nl\/?page_id=80\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Year 1 &#8211; Block A\",\"item\":\"https:\/\/aaidm.buas.nl\/?page_id=82\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"PR1: Introduction to Programming\",\"item\":\"https:\/\/aaidm.buas.nl\/?page_id=99\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"PR1 &#8211; Preparation for Week 8 &#8211; Writing a file\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/aaidm.buas.nl\/#website\",\"url\":\"https:\/\/aaidm.buas.nl\/\",\"name\":\"Applied Artificial Intelligence and Data Management\",\"description\":\"Course Information\",\"publisher\":{\"@id\":\"https:\/\/aaidm.buas.nl\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/aaidm.buas.nl\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/aaidm.buas.nl\/#organization\",\"name\":\"Applied Artificial Intelligence and Data Management\",\"url\":\"https:\/\/aaidm.buas.nl\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/aaidm.buas.nl\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/aaidm.buas.nl\/wp-content\/uploads\/2020\/06\/buas-logo.png\",\"contentUrl\":\"https:\/\/aaidm.buas.nl\/wp-content\/uploads\/2020\/06\/buas-logo.png\",\"width\":382,\"height\":132,\"caption\":\"Applied Artificial Intelligence and Data Management\"},\"image\":{\"@id\":\"https:\/\/aaidm.buas.nl\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PR1 - Preparation for Week 8 - Writing a file - Applied Artificial Intelligence and Data Management","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/aaidm.buas.nl\/?page_id=398","og_locale":"en_US","og_type":"article","og_title":"PR1 - Preparation for Week 8 - Writing a file - Applied Artificial Intelligence and Data Management","og_description":"uthor: Carlos Santos Learning Line: Programming Course: PR1: Introduction to Programming Week: 8 Competencies: Students will be able to effectively define and use variables, programming flow control. BoKS: \u00ad 3bK2, The student understands the principles of data related software like Python, R, Scala and\/or Java and knows how to write (basic) scripts. Learning Goals: Able [&hellip;]","og_url":"https:\/\/aaidm.buas.nl\/?page_id=398","og_site_name":"Applied Artificial Intelligence and Data Management","article_modified_time":"2020-08-26T15:37:26+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/aaidm.buas.nl\/?page_id=398","url":"https:\/\/aaidm.buas.nl\/?page_id=398","name":"PR1 - Preparation for Week 8 - Writing a file - Applied Artificial Intelligence and Data Management","isPartOf":{"@id":"https:\/\/aaidm.buas.nl\/#website"},"datePublished":"2020-08-26T15:24:12+00:00","dateModified":"2020-08-26T15:37:26+00:00","breadcrumb":{"@id":"https:\/\/aaidm.buas.nl\/?page_id=398#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/aaidm.buas.nl\/?page_id=398"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/aaidm.buas.nl\/?page_id=398#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/aaidm.buas.nl\/"},{"@type":"ListItem","position":2,"name":"Year 1","item":"https:\/\/aaidm.buas.nl\/?page_id=80"},{"@type":"ListItem","position":3,"name":"Year 1 &#8211; Block A","item":"https:\/\/aaidm.buas.nl\/?page_id=82"},{"@type":"ListItem","position":4,"name":"PR1: Introduction to Programming","item":"https:\/\/aaidm.buas.nl\/?page_id=99"},{"@type":"ListItem","position":5,"name":"PR1 &#8211; Preparation for Week 8 &#8211; Writing a file"}]},{"@type":"WebSite","@id":"https:\/\/aaidm.buas.nl\/#website","url":"https:\/\/aaidm.buas.nl\/","name":"Applied Artificial Intelligence and Data Management","description":"Course Information","publisher":{"@id":"https:\/\/aaidm.buas.nl\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/aaidm.buas.nl\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/aaidm.buas.nl\/#organization","name":"Applied Artificial Intelligence and Data Management","url":"https:\/\/aaidm.buas.nl\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/aaidm.buas.nl\/#\/schema\/logo\/image\/","url":"https:\/\/aaidm.buas.nl\/wp-content\/uploads\/2020\/06\/buas-logo.png","contentUrl":"https:\/\/aaidm.buas.nl\/wp-content\/uploads\/2020\/06\/buas-logo.png","width":382,"height":132,"caption":"Applied Artificial Intelligence and Data Management"},"image":{"@id":"https:\/\/aaidm.buas.nl\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/pages\/398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=398"}],"version-history":[{"count":2,"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/pages\/398\/revisions"}],"predecessor-version":[{"id":401,"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/pages\/398\/revisions\/401"}],"up":[{"embeddable":true,"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/pages\/99"}],"wp:attachment":[{"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}