1
00:00:00,810 --> 00:00:01,980
-: Up until this point,

2
00:00:01,980 --> 00:00:04,770
we've learned how to read files,

3
00:00:04,770 --> 00:00:06,120
but we had this annoying thing

4
00:00:06,120 --> 00:00:07,080
with the cursor

5
00:00:07,080 --> 00:00:10,200
and then we had to close the file.

6
00:00:10,200 --> 00:00:12,540
So how can we fix this?

7
00:00:12,540 --> 00:00:14,970
Well, a better way to actually do something

8
00:00:14,970 --> 00:00:16,170
like this and do file io

9
00:00:16,170 --> 00:00:18,420
with Python is

10
00:00:18,420 --> 00:00:20,850
with the built in, with statement.

11
00:00:20,850 --> 00:00:22,200
Let me show you,

12
00:00:22,200 --> 00:00:24,030
instead of what we had before,

13
00:00:24,030 --> 00:00:25,743
what we're gonna say is,

14
00:00:26,820 --> 00:00:30,483
with open, the name of the file,

15
00:00:31,740 --> 00:00:34,950
and say as, whatever the variable you want.

16
00:00:34,950 --> 00:00:39,720
So my file, so now with open,

17
00:00:39,720 --> 00:00:41,520
you can actually do something like this,

18
00:00:41,520 --> 00:00:43,863
but you don't have to worry about closing.

19
00:00:44,730 --> 00:00:46,290
So, most of the time,

20
00:00:46,290 --> 00:00:47,940
you want to use the with statement,

21
00:00:47,940 --> 00:00:49,839
this is the proper way

22
00:00:49,839 --> 00:00:51,990
to work with files in Python.

23
00:00:51,990 --> 00:00:53,460
And if I save here

24
00:00:53,460 --> 00:00:56,523
and run my script,

25
00:00:57,660 --> 00:00:58,500
oh, I get an error,

26
00:00:58,500 --> 00:01:01,410
because I forgot the colon here.

27
00:01:01,410 --> 00:01:03,630
If I run again, there you go.

28
00:01:03,630 --> 00:01:05,129
I get my output,

29
00:01:05,129 --> 00:01:07,560
and I don't need to close my file.

30
00:01:07,560 --> 00:01:09,210
Everything is done for me.

31
00:01:09,210 --> 00:01:11,553
So this is a standard way to read a file.

32
00:01:12,810 --> 00:01:15,270
Now, what if we wanted to write to a file?

33
00:01:15,270 --> 00:01:17,310
So far we've only read,

34
00:01:17,310 --> 00:01:19,290
and actually, underneath the hood,

35
00:01:19,290 --> 00:01:22,590
open, has a default parameter,

36
00:01:22,590 --> 00:01:27,270
called mode, equals, two, R,

37
00:01:27,270 --> 00:01:29,553
which stands for read.

38
00:01:31,140 --> 00:01:33,330
So with mode R,

39
00:01:33,330 --> 00:01:35,430
we are able to read files.

40
00:01:35,430 --> 00:01:37,800
When we don't specify the mode,

41
00:01:37,800 --> 00:01:39,540
it automatically just assumes,

42
00:01:39,540 --> 00:01:40,650
we're reading.

43
00:01:40,650 --> 00:01:43,680
But if we wanted to write to a file,

44
00:01:43,680 --> 00:01:47,313
we can simply do W, for write.

45
00:01:48,180 --> 00:01:50,073
So if I run this,

46
00:01:52,110 --> 00:01:53,550
you see that I get an error now,

47
00:01:53,550 --> 00:01:56,640
unsupported operation, not readable,

48
00:01:56,640 --> 00:01:57,870
because, well,

49
00:01:57,870 --> 00:02:00,063
the file can only be written.

50
00:02:01,020 --> 00:02:03,510
So, in order for me to read and write,

51
00:02:03,510 --> 00:02:05,880
I can write r w, right?

52
00:02:05,880 --> 00:02:06,713
Well, let's see.

53
00:02:09,300 --> 00:02:11,820
Nope, that's actually not how it works.

54
00:02:11,820 --> 00:02:13,350
You would think it'll be rw,

55
00:02:13,350 --> 00:02:15,003
but this is a bit of a trick.

56
00:02:15,990 --> 00:02:17,730
In order to read and write,

57
00:02:17,730 --> 00:02:19,863
we do read plus,

58
00:02:21,510 --> 00:02:23,463
so that if I run this,

59
00:02:24,750 --> 00:02:29,310
all right, I'm reading and writing,

60
00:02:29,310 --> 00:02:30,930
and I get an empty list,

61
00:02:30,930 --> 00:02:33,930
because this is now empty.

62
00:02:33,930 --> 00:02:36,123
Okay, so let's write to a file here.

63
00:02:37,890 --> 00:02:39,030
Let's make this a little bit smaller,

64
00:02:39,030 --> 00:02:40,953
just so we can see, that's better.

65
00:02:43,080 --> 00:02:44,910
Let's say, that in this file,

66
00:02:44,910 --> 00:02:47,010
I wanna have text,

67
00:02:47,010 --> 00:02:49,230
and in this text,

68
00:02:49,230 --> 00:02:52,773
I am going to say my file dot write,

69
00:02:54,390 --> 00:02:57,510
and I can write whatever piece of text I want.

70
00:02:57,510 --> 00:03:00,553
So let's say, hey, it's me.

71
00:03:06,240 --> 00:03:07,920
So now in here,

72
00:03:07,920 --> 00:03:11,073
if I do print, text,

73
00:03:11,970 --> 00:03:16,970
and I run this, I get 11.

74
00:03:17,100 --> 00:03:18,693
Hmm, what is this 11?

75
00:03:21,150 --> 00:03:23,790
Well, this isn't really important right now.

76
00:03:23,790 --> 00:03:25,140
What we wanna focus on is,

77
00:03:25,140 --> 00:03:26,700
hey, it's me,

78
00:03:26,700 --> 00:03:28,320
I've written to a file.

79
00:03:28,320 --> 00:03:31,560
Okay, so let's say I wanted to open the file again,

80
00:03:31,560 --> 00:03:33,720
and actually write again.

81
00:03:33,720 --> 00:03:34,830
Let's say this time around,

82
00:03:34,830 --> 00:03:37,413
I'm going to write a smiley face.

83
00:03:38,400 --> 00:03:40,443
If I run this,

84
00:03:41,580 --> 00:03:43,650
and go back,

85
00:03:43,650 --> 00:03:48,650
alright, it looks like I wrote to the file,

86
00:03:48,900 --> 00:03:51,123
but, uh oh, is this what we wanted?

87
00:03:51,960 --> 00:03:54,690
When we write something to a file,

88
00:03:54,690 --> 00:03:56,400
the cursor resets.

89
00:03:56,400 --> 00:03:58,800
Remember, when we open a file,

90
00:03:58,800 --> 00:04:01,030
the cursor goes to zero

91
00:04:01,980 --> 00:04:05,010
and we write to the file,

92
00:04:05,010 --> 00:04:07,050
and, if there's something already existing in there,

93
00:04:07,050 --> 00:04:08,490
well, we override it.

94
00:04:08,490 --> 00:04:10,170
So we have to be careful,

95
00:04:10,170 --> 00:04:12,090
and by the way, this text file,

96
00:04:12,090 --> 00:04:15,150
just gave us, that we wrote two characters,

97
00:04:15,150 --> 00:04:16,322
the smiley face.

98
00:04:18,329 --> 00:04:20,519
Okay, so how can we fix this?

99
00:04:20,519 --> 00:04:21,690
How can we make sure

100
00:04:21,690 --> 00:04:24,120
that we don't break what we have previously,

101
00:04:24,120 --> 00:04:25,650
which is, hey, it's me,

102
00:04:25,650 --> 00:04:28,890
and I just wanna add a smiley face at the end.

103
00:04:28,890 --> 00:04:32,700
Well, we can use the append mode.

104
00:04:32,700 --> 00:04:35,790
The append mode, is a,

105
00:04:35,790 --> 00:04:38,100
and this appends to the end of the file.

106
00:04:38,100 --> 00:04:40,080
If I run this, look at that,

107
00:04:40,080 --> 00:04:42,030
hey, it's me, with a smiley face,

108
00:04:42,030 --> 00:04:43,890
it appends to the end.

109
00:04:43,890 --> 00:04:46,260
So, that's the append mode.

110
00:04:46,260 --> 00:04:49,410
Again, we can use W to write,

111
00:04:49,410 --> 00:04:50,553
and if I run this,

112
00:04:52,020 --> 00:04:55,200
it adds a smiley face.

113
00:04:55,200 --> 00:04:58,470
Now, why did it not do like last time,

114
00:04:58,470 --> 00:05:00,453
and overwrite the existing thing?

115
00:05:01,440 --> 00:05:03,573
Well, it's because of our mode.

116
00:05:05,340 --> 00:05:09,330
W allows us to write to a file.

117
00:05:09,330 --> 00:05:12,540
Previously, when we did read and write,

118
00:05:12,540 --> 00:05:13,770
what happened was,

119
00:05:13,770 --> 00:05:18,630
we read the file, let's run this again.

120
00:05:18,630 --> 00:05:19,800
Actually, let's go back to,

121
00:05:19,800 --> 00:05:21,750
hey, it's me, the way it was.

122
00:05:21,750 --> 00:05:23,223
And if I run this again,

123
00:05:26,040 --> 00:05:28,893
because we did read and write,

124
00:05:29,910 --> 00:05:32,970
the cursor was set to zero

125
00:05:32,970 --> 00:05:34,140
and overrode it,

126
00:05:34,140 --> 00:05:37,113
versus, if we just did the write,

127
00:05:38,010 --> 00:05:40,380
it will assume this is a new file,

128
00:05:40,380 --> 00:05:44,640
and we just wanna, well, just add the smiley face.

129
00:05:44,640 --> 00:05:46,290
If I click run here,

130
00:05:46,290 --> 00:05:48,540
you see that now is just smiley face.

131
00:05:48,540 --> 00:05:49,620
So you wanna be careful

132
00:05:49,620 --> 00:05:51,570
that you use the right mode,

133
00:05:51,570 --> 00:05:52,833
based on what you need.

134
00:05:53,940 --> 00:05:56,040
Now, let's say we wanted to create a file

135
00:05:56,040 --> 00:05:57,063
that doesn't exist.

136
00:05:58,530 --> 00:06:02,883
Let's have a file called, happy dot text.

137
00:06:03,990 --> 00:06:05,440
Or actually no, let's do sad.

138
00:06:06,390 --> 00:06:08,790
And this writes a sad face,

139
00:06:08,790 --> 00:06:10,770
but remember, we don't have this on our desktop.

140
00:06:10,770 --> 00:06:11,760
If I clear this,

141
00:06:11,760 --> 00:06:13,110
and look at our desktop,

142
00:06:13,110 --> 00:06:15,450
we don't have a sad dot text.

143
00:06:15,450 --> 00:06:17,460
If I do read plus here,

144
00:06:17,460 --> 00:06:19,140
so read and write,

145
00:06:19,140 --> 00:06:20,163
and I run this,

146
00:06:22,320 --> 00:06:24,783
I get a file not found error.

147
00:06:25,740 --> 00:06:28,920
If I do a write here,

148
00:06:28,920 --> 00:06:32,340
and I run this, right, it looks like it worked.

149
00:06:32,340 --> 00:06:36,750
If I look at the files, I have a sad text now.

150
00:06:36,750 --> 00:06:38,283
So now if I open it,

151
00:06:39,990 --> 00:06:42,423
I have it, sad face text.

152
00:06:43,470 --> 00:06:46,890
So write also, creates a new file,

153
00:06:46,890 --> 00:06:48,420
if it doesn't exist already,

154
00:06:48,420 --> 00:06:50,580
or overwrites the existing one.

155
00:06:50,580 --> 00:06:52,170
So you wanna be careful, once again,

156
00:06:52,170 --> 00:06:54,450
with the mode, if whether you wanna append,

157
00:06:54,450 --> 00:06:55,650
whether you wanna read,

158
00:06:55,650 --> 00:06:57,810
whether you wanna read and write.

159
00:06:57,810 --> 00:06:58,860
But, most of the time

160
00:06:58,860 --> 00:07:03,120
the most common ones are read, write, and append.

161
00:07:03,120 --> 00:07:04,560
Now, in the next video,

162
00:07:04,560 --> 00:07:08,370
I wanna talk about this idea of a file.

163
00:07:08,370 --> 00:07:10,620
I've written text files, for now,

164
00:07:10,620 --> 00:07:12,210
but we could have written anything.

165
00:07:12,210 --> 00:07:14,220
We could have done a dot py file,

166
00:07:14,220 --> 00:07:15,450
if I wanted to,

167
00:07:15,450 --> 00:07:17,700
but, what about accessing files

168
00:07:17,700 --> 00:07:19,260
that are in different locations,

169
00:07:19,260 --> 00:07:21,120
such as inside of folders?

170
00:07:21,120 --> 00:07:22,290
We're gonna explore that topic

171
00:07:22,290 --> 00:07:23,190
in the next video.

