{"id":478,"date":"2023-02-23T13:32:43","date_gmt":"2023-02-23T13:32:43","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=478"},"modified":"2023-02-23T13:34:24","modified_gmt":"2023-02-23T13:34:24","slug":"reading-from-a-file","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/reading-from-a-file\/","title":{"rendered":"Reading from a file"},"content":{"rendered":"\n<p>To read the data from an opened file handler you can use the read() function. This returns the contents of the file which you would normally assign to a variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.txt\") as input_file:\n    file_contents = input_file.read()\n\nprint(file_contents)<\/code><\/pre>\n\n\n\n<p>However, most files are large and storing all the data in a single variable will make it harder to process. To work around this, you can use either the splitlines() or readlines() functions, or by using a for loop. These break the file into individual lines by looking for newline characters within a file. As such they will not be useful for binary files.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.txt\") as input_file:\n    file_lines_array = input_file.read().splitlines()\n\n    for line in file_lines_array:\n        print(line)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.txt\") as input_file:\n    file_lines_array = input_file.readlines()\n\n    for line in file_lines_array:\n        print(line)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.txt\") as input_file:\n    for line in input_file:\n        print(line)<\/code><\/pre>\n\n\n\n<p>The splitlines() example removes the \\n newline character from the line whereas the other two do not.<\/p>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/writing-to-a-file\/\" data-type=\"page\" data-id=\"482\">Next: Writing to a file<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To read the data from an opened file handler you can use the read() function. This returns the contents of the file which you would normally assign to a variable. However, most files are large and storing all the data in a single variable will make it harder to process. To work around this, you&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/reading-from-a-file\/\">Continue reading <span class=\"screen-reader-text\">Reading from a file<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":20,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-478","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/478","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=478"}],"version-history":[{"count":4,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/478\/revisions"}],"predecessor-version":[{"id":485,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/478\/revisions\/485"}],"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=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}