{"id":472,"date":"2023-02-23T13:26:15","date_gmt":"2023-02-23T13:26:15","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=472"},"modified":"2023-02-23T13:33:01","modified_gmt":"2023-02-23T13:33:01","slug":"file-handling","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/file-handling\/","title":{"rendered":"File handling"},"content":{"rendered":"\n<p>A common requirement in programming is to be able to read from or write to a file. This allows a program to read a large amount of data, look at a configuration file, or write data to a log or report for further study. To use file handling you use the open() function.&nbsp; This takes the format:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>variable_to_store_file_contents = open(\"name_of_file\", \"XY\")<\/code><\/pre>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>Where X is one of:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Value<\/td><td>Meaning<\/td><\/tr><tr><td>r<\/td><td>Read from a file. Gives an error if the file does not exist.<\/td><\/tr><tr><td>w<\/td><td>Write to a file. Creates the file if it does not exist.<\/td><\/tr><tr><td>a<\/td><td>Appends onto a file. Creates the file if it does not exist.<\/td><\/tr><tr><td>x<\/td><td>Creates a file. Gives an error if the file already exists<\/td><\/tr><tr><td>r+<\/td><td>Reading and writing to a file. Gives an error if the file does not exist.<\/td><\/tr><tr><td>w+<\/td><td>Reading and writing to a file. Creates a file if it does not exist.<\/td><\/tr><tr><td>a+<\/td><td>Reading and writing to a file. Creates the file if it does not exist.<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>And Y is one of:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Value<\/td><td>Meaning<\/td><\/tr><tr><td>t<\/td><td>Text file<\/td><\/tr><tr><td>b<\/td><td>Binary file (images, videos, etc)<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>The default modes are r and t. Reading a text file. You can omit these values if that is what you are trying to do. All three of the following commands are the same:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>input_file = open(\"data.txt\")\ninput_file = open(\"data.txt\", \"r\")\ninput_file = open(\"data.txt\", \"rt\")\n<\/code><\/pre>\n\n\n\n<p>Writing to a file will delete anything currently there so take care when using it and consider appending instead.<\/p>\n\n\n\n<p>Once you have finished using a file you should close the file handler by using the close() function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>input_file = open(\"data.txt\")\n# Carry out operations\ninput_file.close()<\/code><\/pre>\n\n\n\n<p>If file handlers should always be closed, how do you avoid problems if an error occurs in the program before the close() function is called? You should use the&nbsp;<strong>with<\/strong>&nbsp;keyword. The&nbsp;<strong>with<\/strong>&nbsp;keyword automatically closes the file handler when the operations are finished. It also closes it, should an exception occur.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.txt\") as input_file:\n    # Carry out operations<\/code><\/pre>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/reading-from-a-file\/\" data-type=\"page\" data-id=\"478\">Next: Reading from a file<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common requirement in programming is to be able to read from or write to a file. This allows a program to read a large amount of data, look at a configuration file, or write data to a log or report for further study. To use file handling you use the open() function.&nbsp; This takes&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/file-handling\/\">Continue reading <span class=\"screen-reader-text\">File handling<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":20,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-472","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/472","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=472"}],"version-history":[{"count":3,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/472\/revisions"}],"predecessor-version":[{"id":480,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/472\/revisions\/480"}],"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=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}