1
00:00:01,410 --> 00:00:06,700
So this is how you open a text file with your favorite text editor reprogram and let me show you how

2
00:00:06,700 --> 00:00:08,730
to do the same with Python.

3
00:00:08,770 --> 00:00:11,910
Of course the experience is a bit different.

4
00:00:11,920 --> 00:00:14,590
It's not about double clicking the file.

5
00:00:14,680 --> 00:00:16,480
It's about writing code.

6
00:00:16,630 --> 00:00:22,930
So when you open that file with your text editor what happens is that some object is created in your

7
00:00:22,930 --> 00:00:26,180
temporary memory off your computer, in a ram.

8
00:00:26,440 --> 00:00:28,770
So the file was on the hard disk.

9
00:00:28,840 --> 00:00:34,160
When you open it it's created as some type of object in the RAM.

10
00:00:34,180 --> 00:00:36,840
Same thing happens with Python.

11
00:00:36,880 --> 00:00:40,050
In this case you might want to store that object.

12
00:00:40,120 --> 00:00:42,750
That file object that you're going to create in Python.

13
00:00:42,970 --> 00:00:48,840
You might want to store it in a variable, let's call it my file.

14
00:00:49,510 --> 00:00:53,630
The open function is what creates file objects.

15
00:00:53,800 --> 00:00:57,950
And since it is a function it will get an argument.

16
00:00:57,970 --> 00:01:06,630
Now this basics that's pi file is in the same directory with the fruits.txt file, please download

17
00:01:06,630 --> 00:01:09,840
the fruits.txt file which looks like this.

18
00:01:09,960 --> 00:01:11,020
Then let me split it here.

19
00:01:11,020 --> 00:01:18,610
You can drag a file on the side here to have two windows on Visual Studio Code editor, but you can do

20
00:01:18,610 --> 00:01:20,710
that even in other editors.

21
00:01:20,710 --> 00:01:29,110
I'm sure if you're using other editors, so this file for now that'd be in the same folder with 

22
00:01:29,250 --> 00:01:30,260
basics

23
00:01:30,260 --> 00:01:31,190
dot py.

24
00:01:31,360 --> 00:01:38,030
Once you do that then the input for this function is the path to the file.

25
00:01:38,140 --> 00:01:40,880
In this case that would be fruits.txt.

26
00:01:40,950 --> 00:01:46,670
Since the fruits.txt file is in the same directory with 

27
00:01:46,690 --> 00:01:47,680
basics.py.

28
00:01:47,770 --> 00:01:53,410
Now to read the contents or fruits.txt in your text editor like we did graphically it just

29
00:01:53,410 --> 00:01:59,920
about double click in the file and then you see the content on the text editor interface. In Python things

30
00:01:59,920 --> 00:02:03,660
are a bit more, they have to be a bit more explicit.

31
00:02:03,700 --> 00:02:09,130
You have to specify what you want to do because this file we have just created a file object.

32
00:02:10,030 --> 00:02:14,740
If we execute that nothing is going to happen.

33
00:02:14,980 --> 00:02:22,780
An object is going to be created in temporary memory and when the execution of the program ends the

34
00:02:22,810 --> 00:02:25,040
object will be deleted from the memory.

35
00:02:25,180 --> 00:02:30,730
Just like you when you open the text file with the text editor and then you close it the object the

36
00:02:30,730 --> 00:02:34,690
file object is deleted from RAM, from the temporary memory.

37
00:02:34,690 --> 00:02:43,120
So if you want to read what you might want to do is print out my file.

38
00:02:43,120 --> 00:02:46,840
Now my file has a read method.

39
00:02:47,110 --> 00:02:51,330
So this is the object, the file object which has a read method.

40
00:02:51,490 --> 00:02:53,230
It doesn't take any arguments.

41
00:02:53,230 --> 00:02:57,570
So just leave it like that save, execute.

42
00:02:57,580 --> 00:03:00,270
And here you get the outputs of the file.

43
00:03:00,280 --> 00:03:03,430
This is how you read a file in Python.

