1
00:00:01,540 --> 00:00:12,360
Let's take a look at the help documentation of open and you'll see that the open function gets a bunch

2
00:00:12,360 --> 00:00:13,410
of arguments.

3
00:00:13,530 --> 00:00:21,360
The first two are the most important ones, we already used file which is the path to the file object

4
00:00:21,930 --> 00:00:30,680
and for these other ones we use their default values which means for modes we used r, r means read.

5
00:00:30,750 --> 00:00:39,800
Therefore this code here is the same as this.

6
00:00:40,050 --> 00:00:47,960
So you get the same output. Which one should you use?
Well, to make the code simpler and shorter

7
00:00:47,970 --> 00:00:55,170
this is fine, to make the code more explicit in terms of readability so that others can see that this

8
00:00:55,170 --> 00:00:56,680
is about reading the code,

9
00:00:56,790 --> 00:00:59,820
then you'd use this, either way is fine.

10
00:00:59,820 --> 00:01:11,790
Now what can we use for writing for writing you'd use a W for write and let me create a new file under

11
00:01:11,790 --> 00:01:13,310
the files folder.

12
00:01:13,440 --> 00:01:19,730
Call it vegetables, so a vegetables file doesn't exist currently in my files folder.

13
00:01:19,770 --> 00:01:26,400
It would be created by the open function and then in that empty file we were going to write something.

14
00:01:26,930 --> 00:01:27,720
Write.

15
00:01:30,050 --> 00:01:31,010
Tomato.

16
00:01:31,220 --> 00:01:38,450
Of course the contents variable doesn't make sense here because we are not fetching some data from the

17
00:01:38,450 --> 00:01:41,060
file we're just writing there.

18
00:01:41,060 --> 00:01:42,450
So let's keep it like that.

19
00:01:42,450 --> 00:01:53,790
Save it, executes, and a vegetables.txt file has been created in my computer with the tomato string

20
00:01:53,790 --> 00:01:54,180
there.

21
00:01:54,780 --> 00:01:55,980
Let me put it here.

22
00:01:56,910 --> 00:01:59,760
So the read method looks for an existing file.

23
00:01:59,760 --> 00:02:09,330
The write method creates a new file, and I should warn you that if the file exists like in this case

24
00:02:09,330 --> 00:02:11,920
fruits.txt is there,

25
00:02:12,000 --> 00:02:17,070
if that is the case, Python will overwrite the existing file.

26
00:02:17,180 --> 00:02:25,450
So if you open fruits.txt now, you'll see that you have this new contents of the file so the

27
00:02:25,450 --> 00:02:31,180
existing content is discarded.
How can we write more lines?

28
00:02:31,380 --> 00:02:36,500
Well you have to use the \n special string which creates brake lines.

29
00:02:42,240 --> 00:02:44,360
Onion, executes.

30
00:02:45,570 --> 00:02:47,870
So this is fruits.txt.

31
00:02:51,510 --> 00:03:01,870
and you get these three lines as we expected something you should know is that you can apply more than

32
00:03:01,870 --> 00:03:04,490
one method to the open file.

33
00:03:04,840 --> 00:03:13,600
Let's write garlic here, now intentionally I didn't do something I was supposed to do to show you what

34
00:03:13,600 --> 00:03:14,440
happens.

35
00:03:14,440 --> 00:03:23,030
So garlic was immediately put after onion because there was no space or a \n operator

36
00:03:23,050 --> 00:03:23,870
there.

37
00:03:23,980 --> 00:03:29,990
If I put a space here you of course have gets a space visit between onion and garlic.

38
00:03:30,190 --> 00:03:39,040
But what you want instead is you want that after each line you write you want a \n,  and in that

39
00:03:39,040 --> 00:03:41,960
case you'd get the correct output.

40
00:03:41,980 --> 00:03:47,890
So now you're probably wondering well I have this fruits.txt file? I don't want to overwrite.

41
00:03:47,890 --> 00:03:55,430
How can I actually add another line under this existing lines?
And I'll show you how to do that

42
00:03:55,450 --> 00:03:56,020
in the next video.

