{"id":802,"date":"2024-08-13T20:36:07","date_gmt":"2024-08-13T15:36:07","guid":{"rendered":"https:\/\/augurytech.co.uk\/courses\/?p=802"},"modified":"2024-10-05T09:04:43","modified_gmt":"2024-10-05T04:04:43","slug":"python-files-handling","status":"publish","type":"post","link":"https:\/\/augurytech.co.uk\/courses\/python\/python-files-handling\/","title":{"rendered":"Python: Files Handling"},"content":{"rendered":"\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p>None of the data has been preserved in all the coding so far. We created variables, lists, dictionaries, and class instances that contained information, but it disappeared when the computer was turned off.<br>You know how to save a word-processing document or spreadsheet, but how do you save data processed by Python?<\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h4 class=\"wp-block-heading\" style=\"font-style:normal;font-weight:600\">Reading Writing in an external file from Python Code<\/h4>\n\n\n\n<p>We can write in a text file by using a Python function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>open(\"file_name.txt\",\"mode\")<\/strong><\/code><\/pre>\n\n\n\n<p>There are three modes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Read<\/strong><\/li>\n\n\n\n<li><strong>Write<\/strong><\/li>\n\n\n\n<li><strong>Append<\/strong><\/li>\n<\/ul>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Writing to a Text File<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>file = open(\"myFile.txt\", \"w\")\nfile.write(\"This is my file\")<\/strong><\/code><\/pre>\n\n\n\n<p><strong>Note: If a file does not exist, &#8220;w&#8221; mode creates and writes in it.<\/strong><\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Reading from a Text File<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>file = open(\"myFile.txt\", \"r\")\nprint(file.read())<\/strong><\/code><\/pre>\n\n\n\n<p><strong>Note: If the file does not exist, &#8220;r&#8221; mode will throw file not found error<\/strong><\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Writing a file in append mode<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>file = open(\"myFile.txt\", \"a\")\nfile.write(\"This text is written in append mode\")<\/strong><\/code><\/pre>\n\n\n\n<p><strong>Note: in &#8220;w&#8221; mode if we write in the same file all previous work will be overwritten. But append mode allows you to write further.<\/strong><\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>None of the data has been preserved in all the coding so far. We created variables, lists, dictionaries, and class instances that contained information, but it disappeared when the computer was turned off.You know how to save a word-processing document or spreadsheet, but how do you save data processed by Python? Reading Writing in an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-python","format":"standard","meta":{"_seopress_robots_primary_cat":"none","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-802","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python"},"_links":{"self":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/comments?post=802"}],"version-history":[{"count":18,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/802\/revisions"}],"predecessor-version":[{"id":834,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/802\/revisions\/834"}],"wp:attachment":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/media?parent=802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/categories?post=802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/tags?post=802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}