1 00:00:00,000 --> 00:00:01,281 [No audio] 2 00:00:01,306 --> 00:00:03,360 So this is how you open a text file with 3 00:00:03,360 --> 00:00:05,790 your favorite text editor program, and 4 00:00:05,820 --> 00:00:08,010 let me show you how to do the same with 5 00:00:08,010 --> 00:00:10,620 Python. Of course, experience is a 6 00:00:10,620 --> 00:00:13,080 bit different. It's not about double 7 00:00:13,080 --> 00:00:15,510 clicking the file, it's about writing 8 00:00:15,510 --> 00:00:18,210 code. So when you open that file with 9 00:00:18,210 --> 00:00:20,220 your text editor, what happens is that 10 00:00:20,670 --> 00:00:23,520 some object is created in your temporary 11 00:00:23,550 --> 00:00:26,700 memory of your computer, in RAM. So the 12 00:00:26,700 --> 00:00:29,370 file was on the hard disk. When you open 13 00:00:29,370 --> 00:00:32,580 it, it's created as some type of objects 14 00:00:32,580 --> 00:00:35,760 in the RAM. Same thing happens with 15 00:00:35,820 --> 00:00:38,280 Python. In this case, you might want to 16 00:00:38,280 --> 00:00:41,110 store that object, that file object that 17 00:00:41,135 --> 00:00:43,020 that you're going to create in Python, you 18 00:00:43,020 --> 00:00:44,790 might want to store it in a variable. 19 00:00:46,282 --> 00:00:47,543 Let's call it myfile. 20 00:00:49,371 --> 00:00:50,490 The open function 21 00:00:50,520 --> 00:00:54,420 is what creates file objects, and since 22 00:00:54,420 --> 00:00:56,970 it is a function, it will get an 23 00:00:56,970 --> 00:01:02,010 argument. Now, this basics.py file is in 24 00:01:02,010 --> 00:01:06,121 the same directory with the fruits.txt file, 25 00:01:06,145 --> 00:01:08,355 please download the fruits.txt file, 26 00:01:08,379 --> 00:01:09,704 which looks like this, 27 00:01:09,810 --> 00:01:13,590 or let me place it here, you can drag a file on 28 00:01:13,590 --> 00:01:16,140 the side here to have two windows on 29 00:01:16,140 --> 00:01:18,420 Visual Studio Code Editor, and you can 30 00:01:18,420 --> 00:01:21,330 do that even in other editors, I'm sure, 31 00:01:21,750 --> 00:01:24,000 if you're using other editors. So this 32 00:01:24,000 --> 00:01:27,540 file for now, let it be in the same 33 00:01:27,570 --> 00:01:31,740 folder with basics.py. Once you do 34 00:01:31,740 --> 00:01:34,950 that, then the input for this function 35 00:01:35,010 --> 00:01:38,610 is the path to the file. In this case, 36 00:01:38,610 --> 00:01:42,390 that will be fruits.txt. Since the 37 00:01:42,420 --> 00:01:44,040 fruits.txt file is in the 38 00:01:44,040 --> 00:01:48,090 same directory with basics.py. Now to 39 00:01:48,090 --> 00:01:50,610 read the contents of fruits.txt in 40 00:01:50,610 --> 00:01:52,140 your text editor like we did 41 00:01:52,170 --> 00:01:53,880 graphically, it's just about double 42 00:01:53,880 --> 00:01:55,380 clicking the file and then you see the 43 00:01:55,380 --> 00:01:59,100 content on the Text Editor interface. In 44 00:01:59,100 --> 00:02:01,710 Python, things are a bit more, they have 45 00:02:01,710 --> 00:02:04,020 to be a bit more explicit, you have to 46 00:02:04,020 --> 00:02:05,970 specify what you want to do. Because 47 00:02:06,000 --> 00:02:07,860 this file, we have just created a file 48 00:02:07,860 --> 00:02:13,050 object. If we execute that, nothing is 49 00:02:13,050 --> 00:02:16,050 going to happen. An object is going to 50 00:02:16,050 --> 00:02:18,600 be created in temporary memory, and when 51 00:02:18,600 --> 00:02:22,710 the execution of the program ends, the 52 00:02:22,710 --> 00:02:24,900 object will be deleted from the memory, 53 00:02:25,025 --> 00:02:27,155 just like when you open the text 54 00:02:27,180 --> 00:02:28,800 file with the text editor, and then you 55 00:02:28,800 --> 00:02:31,500 close it, the object, the file object is 56 00:02:31,525 --> 00:02:34,105 deleted from RAM, from the temporary memory. 57 00:02:34,535 --> 00:02:36,965 So if you want to read, what you might 58 00:02:36,990 --> 00:02:43,855 want to do is print out myfile. Now myfile 59 00:02:43,879 --> 00:02:47,670 has a read method. So this is the 60 00:02:47,700 --> 00:02:50,130 object, the file object which has a read 61 00:02:50,130 --> 00:02:52,920 method. It doesn't take any arguments. 62 00:02:53,116 --> 00:02:55,426 So just leave it like that, Save, 63 00:02:56,132 --> 00:02:59,610 execute, and here you get the output of 64 00:02:59,610 --> 00:03:03,420 the file. This is how you read a file in Python. 65 00:03:03,444 --> 00:03:07,780 [No audio]