1 00:00:00,000 --> 00:00:03,148 Okay, so I have these fruits.txt file 2 00:00:03,149 --> 00:00:06,479 in here, which has these four lines, 3 00:00:06,509 --> 00:00:09,569 which are not fruits anyway, but you get 4 00:00:09,569 --> 00:00:12,419 the idea, it's text. We want to add two 5 00:00:12,449 --> 00:00:15,954 more lines to this existing fruits.txt file. 6 00:00:15,979 --> 00:00:18,419 We don't know what is there at 7 00:00:18,419 --> 00:00:21,839 the moment. If you look at the open 8 00:00:22,109 --> 00:00:26,321 documentation, and scroll down, you will see 9 00:00:26,345 --> 00:00:29,496 [No audio] 10 00:00:29,521 --> 00:00:32,129 here that you can pass different 11 00:00:32,129 --> 00:00:36,179 modes in this argument here. So r 12 00:00:36,179 --> 00:00:39,239 which we used already, we used w, and then 13 00:00:39,239 --> 00:00:42,555 you have x. If you use x 14 00:00:42,579 --> 00:00:45,296 [Author typing] 15 00:00:45,321 --> 00:00:48,388 and try to write, let's say, Okra. 16 00:00:49,415 --> 00:00:50,643 If you execute that, 17 00:00:50,667 --> 00:00:52,526 [No audio] 18 00:00:52,550 --> 00:00:53,759 it's going to say that file 19 00:00:53,759 --> 00:00:56,519 exists. So unlike the w mode, which 20 00:00:56,669 --> 00:00:59,369 overwrite a file, x will not overwrite 21 00:00:59,369 --> 00:01:02,009 the file if the file exists. However, it 22 00:01:02,009 --> 00:01:05,729 cannot append content to an existing 23 00:01:05,729 --> 00:01:07,169 file, because it says that the file 24 00:01:07,169 --> 00:01:09,389 exists. Therefore, we jump to the next 25 00:01:09,389 --> 00:01:11,252 mode, which is a. 26 00:01:11,276 --> 00:01:15,454 [No audio] 27 00:01:15,479 --> 00:01:16,709 You see that Okra was 28 00:01:16,739 --> 00:01:19,049 added there, of course, with a small 29 00:01:19,049 --> 00:01:21,329 issue, because there was not a break 30 00:01:21,329 --> 00:01:23,549 line here in the text, in the existing 31 00:01:23,549 --> 00:01:25,439 text file. So that's some information 32 00:01:25,439 --> 00:01:27,479 you should know, and if you know there 33 00:01:27,479 --> 00:01:30,449 is no break line, then you would need to, let 34 00:01:30,449 --> 00:01:32,429 me save this text file, then you'd need 35 00:01:32,429 --> 00:01:37,804 to add a break line there to get the 36 00:01:37,829 --> 00:01:40,529 correct output. So anytime you add a new 37 00:01:40,529 --> 00:01:42,689 line, you'd need to add a break line in 38 00:01:42,689 --> 00:01:48,688 front. Now, how about reading that content? 39 00:01:48,712 --> 00:01:52,085 [No audio] 40 00:01:52,110 --> 00:01:53,789 Maybe saving it a, save it in a 41 00:01:53,789 --> 00:01:58,949 variable and print out the content. Will 42 00:01:58,949 --> 00:02:01,619 it work? I don't think so. It says that 43 00:02:01,619 --> 00:02:04,349 the operation is not supported, the 44 00:02:04,349 --> 00:02:08,459 file is not readable. It's a, which is 45 00:02:08,494 --> 00:02:12,484 for appending. If you wants to append, to 46 00:02:12,509 --> 00:02:15,839 write and it reads at the same time, you 47 00:02:15,864 --> 00:02:17,488 want to add a '+'. 48 00:02:17,512 --> 00:02:20,854 [No audio] 49 00:02:20,879 --> 00:02:21,967 Just like that, 50 00:02:23,399 --> 00:02:27,659 execute, you get an empty output. Why? 51 00:02:28,414 --> 00:02:30,964 Because of the cursor. So what's 52 00:02:30,989 --> 00:02:34,229 happened is that when we added the 53 00:02:34,259 --> 00:02:38,579 Okra line there, so at first you 54 00:02:38,579 --> 00:02:40,979 created the file, here at this point, 55 00:02:41,159 --> 00:02:44,429 the cursor is here, okay? Then you write 56 00:02:44,429 --> 00:02:48,719 the file. In this line, the cursor goes 57 00:02:48,999 --> 00:02:50,355 to the ends of the file. 58 00:02:50,379 --> 00:02:52,379 [No audio] 59 00:02:52,404 --> 00:02:57,011 So when you read now, you'd read whatever is left under 60 00:02:57,089 --> 00:02:59,909 the last line, which is nothing. What 61 00:02:59,934 --> 00:03:05,939 you can do is myfile.seek, apply a seek 62 00:03:05,939 --> 00:03:08,219 method to put the cursor at the 0 63 00:03:08,219 --> 00:03:09,819 position once again. 64 00:03:11,352 --> 00:03:13,589 In that case, the 65 00:03:13,589 --> 00:03:16,169 cursor will go to the end, it will go 66 00:03:16,199 --> 00:03:18,419 again to the 0 position with this 67 00:03:18,419 --> 00:03:21,659 line, and then you'll read everything 68 00:03:21,689 --> 00:03:24,989 from there and down. So you get the 69 00:03:24,989 --> 00:03:26,429 correct output. 70 00:03:26,453 --> 00:03:30,206 [No audio]