1 00:00:00,000 --> 00:00:01,355 [No audio] 2 00:00:01,380 --> 00:00:03,240 Let's take a look at the help 3 00:00:03,270 --> 00:00:07,488 documentation of open, 4 00:00:09,160 --> 00:00:12,485 and you'll see that the open function gets a bunch of 5 00:00:12,510 --> 00:00:14,970 arguments. The first two are the most 6 00:00:14,970 --> 00:00:17,790 important ones. We already used file, 7 00:00:18,690 --> 00:00:21,390 which is the path to the file object, 8 00:00:21,792 --> 00:00:24,245 and for these other ones, we use their 9 00:00:24,270 --> 00:00:27,450 default values, which means for modes, we 10 00:00:27,450 --> 00:00:32,550 used r, r means read, therefore, this 11 00:00:32,585 --> 00:00:34,569 quote here is the same as 12 00:00:34,593 --> 00:00:36,593 [No audio] 13 00:00:36,602 --> 00:00:37,702 this. 14 00:00:37,726 --> 00:00:39,903 [No audio] 15 00:00:39,948 --> 00:00:44,015 So you get the same output. Which one should 16 00:00:44,040 --> 00:00:46,230 you use? Well, for to make the code 17 00:00:46,320 --> 00:00:49,200 simpler and shorter, this is fine. To 18 00:00:49,200 --> 00:00:52,290 make the code more explicit in terms of 19 00:00:52,290 --> 00:00:54,900 readability so that others can see that 20 00:00:54,900 --> 00:00:56,970 this is about reading the code, then 21 00:00:56,970 --> 00:01:00,030 you'd use this, either way is fine. Now 22 00:01:00,030 --> 00:01:02,820 what can we use for writing? For writing, 23 00:01:02,820 --> 00:01:06,415 you'd use w for write and let me 24 00:01:06,440 --> 00:01:08,857 [No audio] 25 00:01:08,882 --> 00:01:13,380 create a new file under the files folder, call 26 00:01:13,380 --> 00:01:16,500 it vegetables. So a vegetables file 27 00:01:16,500 --> 00:01:19,020 doesn't exist currently in my files 28 00:01:19,020 --> 00:01:22,170 folder. This will be created by the open 29 00:01:22,200 --> 00:01:24,870 function. And then in that empty file, 30 00:01:24,900 --> 00:01:27,720 we're going to write something, write 31 00:01:27,744 --> 00:01:29,945 [No audio] 32 00:01:29,970 --> 00:01:33,150 Tomato. Of course, the content variable 33 00:01:33,150 --> 00:01:35,250 doesn't make sense here, because we're 34 00:01:35,250 --> 00:01:38,760 not fetching some data from the file, 35 00:01:38,760 --> 00:01:41,610 we're just writing there. So let's keep 36 00:01:41,635 --> 00:01:43,198 it like that, Save it, 37 00:01:44,860 --> 00:01:46,980 execute, and a 38 00:01:46,980 --> 00:01:49,710 vegetables.txt file has been created 39 00:01:49,950 --> 00:01:53,760 in my computer with a Tomato string 40 00:01:53,760 --> 00:01:57,540 there. Let me put it here. So the read 41 00:01:57,540 --> 00:01:59,850 method looks for an existing file, the 42 00:01:59,850 --> 00:02:03,150 write method creates a new file, and I 43 00:02:03,150 --> 00:02:08,010 should warn you that if the file exists, 44 00:02:08,370 --> 00:02:11,040 like in this case, fruits.txt is 45 00:02:11,040 --> 00:02:15,030 there, if that is the case, Python will 46 00:02:15,060 --> 00:02:17,520 overwrite the existing file. So if you 47 00:02:17,520 --> 00:02:19,740 open fruits.txt now, you'll see that 48 00:02:20,443 --> 00:02:23,099 you have this new content of the file. 49 00:02:23,123 --> 00:02:25,127 [No audio] 50 00:02:25,152 --> 00:02:27,517 So the existing content is discarded. 51 00:02:29,010 --> 00:02:31,680 How can we write more lines? Well, we 52 00:02:31,680 --> 00:02:34,140 have to use the backslash n special 53 00:02:34,140 --> 00:02:36,540 string which creates break lines. 54 00:02:36,564 --> 00:02:42,095 [No audio] 55 00:02:42,120 --> 00:02:47,388 Onion, execute. So this is fruits.txt. 56 00:02:47,412 --> 00:02:51,349 [No audio] 57 00:02:51,374 --> 00:02:54,272 And you get these three lines as we expected. 58 00:02:54,297 --> 00:02:56,314 [No audio] 59 00:02:56,339 --> 00:02:58,102 Something, you should know is that 60 00:02:58,126 --> 00:03:00,237 [No audio] 61 00:03:00,262 --> 00:03:02,760 you can apply more than one method 62 00:03:03,188 --> 00:03:06,597 to the open file. Let's write Garlic here. 63 00:03:06,621 --> 00:03:09,001 [No audio] 64 00:03:09,026 --> 00:03:10,505 Now intentionally, I didn't do 65 00:03:10,530 --> 00:03:12,720 something I was supposed to do to show 66 00:03:12,720 --> 00:03:15,660 you what happens. So Garlic was 67 00:03:15,905 --> 00:03:18,095 immediately put after Onion because 68 00:03:18,120 --> 00:03:22,380 there is no space or a backslash n 69 00:03:22,380 --> 00:03:25,260 operator there. If I put a space here, 70 00:03:25,620 --> 00:03:28,680 you of course, it gets a space between 71 00:03:28,710 --> 00:03:30,840 Onion and Garlic. But what you want 72 00:03:30,840 --> 00:03:35,400 instead is you want that after each line 73 00:03:35,400 --> 00:03:38,850 you write, you want a backslash n, and in 74 00:03:38,850 --> 00:03:40,980 that case, you'd get the correct output. 75 00:03:41,820 --> 00:03:44,220 So now you're probably wondering, well, 76 00:03:44,220 --> 00:03:46,860 I have this fruits.txt file, I don't 77 00:03:46,860 --> 00:03:49,530 want to overwrite it. How can I actually 78 00:03:49,560 --> 00:03:52,890 add another line under this existing 79 00:03:52,890 --> 00:03:55,020 lines? And I'll show you how to do that 80 00:03:55,110 --> 00:03:56,206 in the next video. 81 00:03:56,230 --> 00:03:59,973 [No audio]