1
00:00:00,780 --> 00:00:07,110
Up until this point, we've learned how to read files, but we had this annoying thing with the cursor

2
00:00:07,110 --> 00:00:09,400
and then we had to close the file.

3
00:00:10,170 --> 00:00:11,680
So how can we fix this?

4
00:00:12,540 --> 00:00:19,440
Well, a better way to actually do something like this and do file Eyo with Python is with the built

5
00:00:19,440 --> 00:00:20,780
in with statement.

6
00:00:20,850 --> 00:00:25,560
Let me show you, instead of what we had before, what we're going to say is.

7
00:00:26,800 --> 00:00:30,310
With open the name of the file.

8
00:00:31,720 --> 00:00:36,190
And say as whatever the variable you want, so my file.

9
00:00:37,390 --> 00:00:43,780
So now with Open, you can actually do something like this, but you don't have to worry about closing,

10
00:00:44,710 --> 00:00:47,950
so most of the time you want to use the width statement.

11
00:00:47,960 --> 00:00:51,570
This is the proper way to work with files in Python.

12
00:00:51,970 --> 00:00:54,550
And if I save here and run.

13
00:00:57,590 --> 00:01:02,540
Well, I get an error because I forgot the column here if I run again.

14
00:01:02,750 --> 00:01:03,340
There you go.

15
00:01:03,620 --> 00:01:07,260
I get my output and I don't need to close my file.

16
00:01:07,520 --> 00:01:08,720
Everything is down for me.

17
00:01:09,230 --> 00:01:11,380
So this is a standard way to read a file.

18
00:01:12,790 --> 00:01:15,290
Now, what if we wanted to write to a file?

19
00:01:15,310 --> 00:01:23,710
So far we've only read and actually underneath the Hood Open has a default parameter called MOAD.

20
00:01:24,740 --> 00:01:29,420
Equals to R, which stands for Read.

21
00:01:31,110 --> 00:01:39,570
So with Mode are we are able to read files when we don't specify the mode, it automatically just assumes

22
00:01:39,570 --> 00:01:40,130
we're reading.

23
00:01:40,650 --> 00:01:46,660
But if we wanted to write to a file, we can simply do W for.

24
00:01:46,830 --> 00:01:47,200
Right.

25
00:01:48,160 --> 00:01:49,930
So if I run this.

26
00:01:52,110 --> 00:01:58,740
You see that I get an error now, unsupported operation, not readable because, well, the file can

27
00:01:58,740 --> 00:01:59,940
only be Rinne.

28
00:02:00,980 --> 00:02:05,220
So in order for me to read and write, I can write r w.

29
00:02:05,270 --> 00:02:05,630
Right.

30
00:02:05,900 --> 00:02:06,440
Well, let's see.

31
00:02:09,270 --> 00:02:14,880
No, that's actually not how it works, you would think it be RW, but this is a bit of a trick.

32
00:02:15,950 --> 00:02:19,700
In order to read and write, we do read plus.

33
00:02:21,530 --> 00:02:23,270
So that if I run this.

34
00:02:26,110 --> 00:02:33,790
I'm reading and writing and I got an empty list because this is now empty.

35
00:02:33,940 --> 00:02:35,980
OK, so let's write to a file here.

36
00:02:37,880 --> 00:02:40,760
Let's make this a little bit smaller, just so we can see that's better.

37
00:02:43,090 --> 00:02:52,690
Let's say that in this fall, I want to have text and in this text I'm going to say my file dot, right.

38
00:02:54,340 --> 00:03:00,130
And I can write whatever piece of text I want, so let's say, hey, it's.

39
00:03:06,260 --> 00:03:10,910
So now in here, if I do print text.

40
00:03:11,930 --> 00:03:13,640
And I run this.

41
00:03:17,090 --> 00:03:18,530
Hmm, what does this 11?

42
00:03:21,130 --> 00:03:26,410
Well, this is a really important right now what we want to focus on is, hey, it's me.

43
00:03:26,680 --> 00:03:27,910
I've written to a file.

44
00:03:28,300 --> 00:03:33,700
OK, so let's say I wanted to open the file again and actually write again.

45
00:03:33,730 --> 00:03:37,240
Let's say this time around I'm going to write a smiley face.

46
00:03:38,360 --> 00:03:40,250
If I run this.

47
00:03:41,550 --> 00:03:42,850
And go back.

48
00:03:43,620 --> 00:03:51,000
All right, it looks like I wrote to the file, but, oh, is this what we wanted?

49
00:03:51,930 --> 00:03:56,260
When we write something to a file, the cursor resets.

50
00:03:56,370 --> 00:04:00,960
Remember, when we open a file, the cursor goes to zero.

51
00:04:01,960 --> 00:04:08,260
And we write to the file, and if there's something already existing in there while we overwrite it,

52
00:04:08,500 --> 00:04:09,610
so we have to be careful.

53
00:04:10,120 --> 00:04:16,210
And by the way, this text file just gave us that we wrote two characters, The Smiley Face.

54
00:04:18,290 --> 00:04:24,350
OK, so how can we fix this, how can we make sure that we don't break what we have previously, which

55
00:04:24,350 --> 00:04:28,200
is, hey, it's me and I just want to add a smiley face at the end?

56
00:04:28,880 --> 00:04:32,210
Well, we can use the append mode.

57
00:04:32,720 --> 00:04:37,970
The append mode is a and this A to the end of the file.

58
00:04:38,090 --> 00:04:43,400
If I run this like that, hey, it's me with a smiley face, it happens to the end.

59
00:04:43,910 --> 00:04:45,700
So that's the append mode.

60
00:04:46,220 --> 00:04:50,390
Again, we can use W to write and if I run this.

61
00:04:53,290 --> 00:04:54,620
Adds a smiley face.

62
00:04:55,240 --> 00:05:00,340
Now, why did it not do like last time and overwrite the existing thing?

63
00:05:01,470 --> 00:05:03,450
Well, it's because of our moed.

64
00:05:05,350 --> 00:05:15,940
W. allows us to write to a file previously when we did read and write, what happened was we read the

65
00:05:15,940 --> 00:05:16,370
file.

66
00:05:16,780 --> 00:05:18,910
Let's run this again, actually.

67
00:05:19,060 --> 00:05:21,280
Let's go back to hey, it's me the way it was.

68
00:05:21,700 --> 00:05:23,050
And if I run this again.

69
00:05:26,060 --> 00:05:28,760
Because we did read and write.

70
00:05:29,920 --> 00:05:37,000
The cursor was set to zero and over it versus if we just did the right.

71
00:05:37,960 --> 00:05:44,410
It will assume this is a new file and we just want to well, just add the smiley face.

72
00:05:44,650 --> 00:05:48,400
If I click run here, you see that now is just smiley face.

73
00:05:48,550 --> 00:05:52,720
So you want to be careful that you use the right mode based on what you need.

74
00:05:53,960 --> 00:05:56,930
Now, let's say we wanted to create a father doesn't exist.

75
00:05:58,520 --> 00:06:02,750
Let's have a file called Happy Dot Taxed.

76
00:06:04,000 --> 00:06:05,350
Or actually, no, let's do set.

77
00:06:06,300 --> 00:06:11,970
And this writes a sad face, but remember, we don't have this on our desktop, if I clear this and

78
00:06:11,970 --> 00:06:15,120
look at our desktop, we don't have a sad text.

79
00:06:15,450 --> 00:06:17,450
If I do read plus here.

80
00:06:17,460 --> 00:06:19,980
So read and write and I run this.

81
00:06:22,260 --> 00:06:24,630
I get a file not found here.

82
00:06:25,750 --> 00:06:29,770
If I do a right here and I run this.

83
00:06:30,970 --> 00:06:36,080
All right, it looks like it worked, if I look at the files, I have a sad text now.

84
00:06:36,760 --> 00:06:38,170
So now if I open it.

85
00:06:39,940 --> 00:06:42,280
I have it sad face text.

86
00:06:43,430 --> 00:06:50,270
So right also creates a new file, if it doesn't exist already or overwrites the existing one.

87
00:06:50,570 --> 00:06:55,070
So you want to be careful once again with the mode, whether you want to append, whether you want to

88
00:06:55,070 --> 00:06:56,930
read, whether you want to read and write.

89
00:06:57,770 --> 00:07:02,630
But most of the time, the most common ones are read, write and append.

90
00:07:03,110 --> 00:07:07,820
Now, in the next video, I want to talk about this idea of a file.

91
00:07:08,330 --> 00:07:12,260
I've written text files for now, but we could have written anything.

92
00:07:12,260 --> 00:07:14,980
We could have done a file if I wanted to.

93
00:07:15,440 --> 00:07:20,780
But what about accessing files that are in different locations such as inside of folders?

94
00:07:21,150 --> 00:07:23,060
We're going to explore that topic in the next video.
