{"id":496,"date":"2023-02-23T15:23:56","date_gmt":"2023-02-23T15:23:56","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=496"},"modified":"2023-02-27T11:58:37","modified_gmt":"2023-02-27T11:58:37","slug":"reading-and-writing","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/reading-and-writing\/","title":{"rendered":"Reading and writing"},"content":{"rendered":"\n<p>When using the open() function with the + argument (r+, w+, a+) you can both read the contents and write into the file. Because of this you need to keep in mind the current position of the file pointer. Think of the file pointer like the cursor in Microsoft Word. If you start typing, the letters appear at the cursor&#8217;s position. The same thing happens with writing to a file, the contents start at the file pointer&#8217;s position. Each character written moves the files pointer along. When you read from a file it reads the character at the file pointer&#8217;s position and then moves it forward 1.<\/p>\n\n\n\n<p>With r+ and w+ the file pointer will be at the start of the file when you open it, with a+ it will be at the end of the file when you open it. You can find the current position of the pointer using the tell() function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lines_in_file = &#091;]\n\nwith open(\"data2.txt\", \"r+\") as input_file:\n    print(\"The current position is\",input_file.tell())\n    line = input_file.readline()\n    while line:\n        lines_in_file.append(line)\n        print(\"The current position is\",input_file.tell())\n        line = input_file.readline()<\/code><\/pre>\n\n\n\n<p>Gives an output of:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The current position is 0\nThe current position is 25\nThe current position is 42\nThe current position is 62\nThe current position is 87\nThe current position is 103\nThe current position is 123<\/code><\/pre>\n\n\n\n<p>Why don\u2019t the numbers match the number of characters on each line? Remember the hidden new line character \\n. The above code was run on Windows which adds an additional character \\r which is a carriage return to each line. If the file had been generated on Mac or Linux the values would be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The current position is 0\nThe current position is 24\nThe current position is 40\nThe current position is 59\nThe current position is 83\nThe current position is 98\nThe current position is 117<\/code><\/pre>\n\n\n\n<p>To move the file pointer you use the seek() function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>input_file.seek(42)<\/code><\/pre>\n\n\n\n<p>This would place the file pointer at the start of the third line in the data2.txt file Windows example.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p>Open up write.py and change it to read:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"HelloWorld.txt\", \"r+t\") as file_handler:\n    line1 = file_handler.readline()\n    print(\"The position after reading line 1 is \" + str(file_handler.tell()))\n    file_handler.seek(5)\n    file_handler.write(\"XX\" + \"\\n\")\n    file_handler.seek(0)\n    line1 = file_handler.readline()\n    line2 = file_handler.readline()\nprint(\"File altered\")<\/code><\/pre>\n\n\n\n<p>Check the output file and see what is different from before.<\/p>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/comma-separated-values-csv\/\" data-type=\"page\" data-id=\"502\">Next: Comma separated values (CSV)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using the open() function with the + argument (r+, w+, a+) you can both read the contents and write into the file. Because of this you need to keep in mind the current position of the file pointer. Think of the file pointer like the cursor in Microsoft Word. If you start typing, the&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/reading-and-writing\/\">Continue reading <span class=\"screen-reader-text\">Reading and writing<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-496","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/496","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=496"}],"version-history":[{"count":7,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/496\/revisions"}],"predecessor-version":[{"id":508,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/496\/revisions\/508"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}