1
00:00:00,009 --> 00:00:01,029
Welcome back.

2
00:00:01,039 --> 00:00:06,610
Now, before we move on, I wanted to let you know that there are different methods

3
00:00:06,829 --> 00:00:10,399
for writing and reading our files.

4
00:00:10,609 --> 00:00:15,829
Now, so far, we've been dealing with the file dot write method, which is of course,

5
00:00:15,840 --> 00:00:17,020
very straightforward.

6
00:00:17,030 --> 00:00:17,389
So

7
00:00:17,520 --> 00:00:20,950
if I run the program right now, it's gonna create my example dot TXD,

8
00:00:20,959 --> 00:00:24,100
I open it up and there is Hello world now, file

9
00:00:24,245 --> 00:00:27,104
write is useful and it's used

10
00:00:27,204 --> 00:00:33,525
whenever we're, we're writing individual or very simple, our strings to our file.

11
00:00:33,534 --> 00:00:33,875
Hello

12
00:00:33,994 --> 00:00:36,145
world is very straightforward. It's very simple.

13
00:00:36,334 --> 00:00:39,575
But what if we wanted to write like multiple strings or

14
00:00:40,064 --> 00:00:46,205
in order to write large amounts of text file or write will no longer be used,

15
00:00:46,215 --> 00:00:47,944
we'll have to use a different method.

16
00:00:47,955 --> 00:00:48,345
So

17
00:00:48,764 --> 00:00:50,104
as an example, right,

18
00:00:50,580 --> 00:00:54,470
if I had a list of names, so let me create my list names.

19
00:00:54,709 --> 00:00:57,439
Let me add the first name, Alex and

20
00:00:58,319 --> 00:01:02,459
uh let me add the new line character. So

21
00:01:02,990 --> 00:01:04,099
slash N

22
00:01:04,839 --> 00:01:06,500
and let me allow

23
00:01:06,709 --> 00:01:09,389
Python to add the remaining names. So

24
00:01:09,580 --> 00:01:13,540
we have Alex Bob Caroline, Dave and Fred, right?

25
00:01:13,800 --> 00:01:18,260
If I wanted to write these strings, I can no longer use file right.

26
00:01:18,269 --> 00:01:20,260
I'll have to use a different method. So,

27
00:01:20,699 --> 00:01:24,980
what I'll do right now is I'm gonna go with the usual R with

28
00:01:25,230 --> 00:01:27,620
open and then brackets and then remember

29
00:01:27,879 --> 00:01:30,940
we'll have to open up a file example dot CXT.

30
00:01:31,360 --> 00:01:33,620
And then w of course two,

31
00:01:34,239 --> 00:01:35,029
right?

32
00:01:36,400 --> 00:01:37,940
We have a colon

33
00:01:38,480 --> 00:01:42,019
and then I'm gonna say sorry as file and then a colon

34
00:01:42,919 --> 00:01:43,620
and now

35
00:01:44,040 --> 00:01:48,089
you can see file dot write lines

36
00:01:48,360 --> 00:01:50,739
and then uh names.

37
00:01:50,900 --> 00:01:51,589
So

38
00:01:51,709 --> 00:01:54,819
write lines of the file dot Write lines.

39
00:01:54,870 --> 00:01:58,430
This is used whenever we're writing multiple strings or

40
00:01:58,440 --> 00:02:02,480
by writing large pieces of text onto our file.

41
00:02:02,489 --> 00:02:02,819
So

42
00:02:03,029 --> 00:02:04,500
if I want to put them right now

43
00:02:04,849 --> 00:02:07,889
and I open it up, there you go. We have the names right there.

44
00:02:07,900 --> 00:02:11,360
So please do be aware of this

45
00:02:11,710 --> 00:02:13,419
in the future. If you're going to create

46
00:02:13,570 --> 00:02:16,759
advanced programs that will require writing large amounts

47
00:02:16,770 --> 00:02:18,979
of text or strings to a particular file,

48
00:02:19,119 --> 00:02:22,160
you want to go with the file dot Write lines

49
00:02:22,320 --> 00:02:25,669
as opposed to the usual file dot Right.

50
00:02:26,509 --> 00:02:31,800
And just like with the writing, we also have different methods for reading.

51
00:02:32,210 --> 00:02:33,490
Now, so far,

52
00:02:33,649 --> 00:02:37,929
we've been using the very simple uh read the file dot read

53
00:02:38,160 --> 00:02:39,649
and I just copy some code in here.

54
00:02:40,360 --> 00:02:42,149
Let me put this in here real quick.

55
00:02:42,429 --> 00:02:43,729
So, all right,

56
00:02:44,000 --> 00:02:48,369
so far we've been dealing with code like this, right? We will have a file

57
00:02:48,490 --> 00:02:50,009
and it will say content

58
00:02:50,149 --> 00:02:52,059
calls file dot we and

59
00:02:52,190 --> 00:02:54,169
there it is Right. So, if I run the program right now,

60
00:02:54,399 --> 00:02:58,220
very, very simple. We have Alex Bob Caroline, Dave and Fred. Ok.

61
00:02:59,080 --> 00:03:01,580
The thing about using file dot Read

62
00:03:01,800 --> 00:03:03,020
is that

63
00:03:03,220 --> 00:03:06,259
it's gonna read the entire file at once,

64
00:03:06,520 --> 00:03:11,220
which is great whenever you're working with very small files. However,

65
00:03:11,559 --> 00:03:13,929
if you're dealing with large files,

66
00:03:14,149 --> 00:03:16,369
it will still work. Don't get me wrong, it will still work.

67
00:03:16,380 --> 00:03:17,820
It's still gonna read everything.

68
00:03:18,240 --> 00:03:22,240
The problem though is that because it was going to read the entire file at once,

69
00:03:22,520 --> 00:03:25,589
it will have to use a lot of memory

70
00:03:25,820 --> 00:03:29,220
in order to load all the text in that file

71
00:03:29,699 --> 00:03:30,419
at once.

72
00:03:30,649 --> 00:03:31,470
That's the problem.

73
00:03:31,479 --> 00:03:34,059
It's not very efficient whenever you're dealing with large

74
00:03:34,070 --> 00:03:37,119
amounts of text in a file like big files.

75
00:03:37,300 --> 00:03:40,380
So you want to use a different method.

76
00:03:40,520 --> 00:03:41,059
Now

77
00:03:41,820 --> 00:03:42,720
let me show you,

78
00:03:43,509 --> 00:03:46,770
I'm going to create a new program right here and say with open.

79
00:03:48,169 --> 00:03:51,470
And now let's assume that example dot TXT in this

80
00:03:51,479 --> 00:03:53,699
scenario has large amounts of text is a very,

81
00:03:53,710 --> 00:03:54,669
very large file, right?

82
00:03:54,679 --> 00:03:54,990
So

83
00:03:55,309 --> 00:03:59,470
I'm gonna go to the usual vout, you know, add a file name and then R

84
00:03:59,750 --> 00:04:01,419
and then say as file,

85
00:04:02,309 --> 00:04:02,889
OK?

86
00:04:03,779 --> 00:04:04,389
Now

87
00:04:05,179 --> 00:04:07,339
check this out. I'm gonna say line

88
00:04:08,009 --> 00:04:10,300
equals file

89
00:04:10,679 --> 00:04:12,899
dot Readline.

90
00:04:13,660 --> 00:04:18,399
Notice the difference. It's no longer a file dot Read, it's now file dot Read line

91
00:04:18,730 --> 00:04:20,279
and then I can create

92
00:04:21,190 --> 00:04:24,130
my up and say while line,

93
00:04:24,589 --> 00:04:28,279
OK? So while our file is open, it has text in it.

94
00:04:28,540 --> 00:04:29,579
We want

95
00:04:29,700 --> 00:04:31,779
to print the line.

96
00:04:32,660 --> 00:04:33,279
Ok?

97
00:04:33,450 --> 00:04:35,299
And we can also then say line

98
00:04:35,679 --> 00:04:37,179
equals file

99
00:04:39,769 --> 00:04:40,540
dot

100
00:04:40,750 --> 00:04:41,600
read

101
00:04:42,429 --> 00:04:43,130
line.

102
00:04:44,549 --> 00:04:45,250
And here it is.

103
00:04:45,579 --> 00:04:48,170
If I was to run the program again,

104
00:04:48,549 --> 00:04:53,059
you will see right now that we have just about the exact same output,

105
00:04:53,179 --> 00:04:54,790
you're not really going to notice the difference

106
00:04:54,799 --> 00:04:58,089
because we're dealing with a small file regardless.

107
00:04:58,390 --> 00:04:59,380
However,

108
00:04:59,510 --> 00:05:03,010
please do keep in mind in the future just like with the right method.

109
00:05:03,140 --> 00:05:06,970
If you're gonna be dealing with large amount of files that you need to read from,

110
00:05:07,429 --> 00:05:09,649
you want to use dot Readline,

111
00:05:09,829 --> 00:05:12,529
if it's a very small file, file dot read

112
00:05:12,640 --> 00:05:15,700
would be used. Ok? It's ideal for those for those kind of scenarios.

113
00:05:15,779 --> 00:05:17,799
However, we are not done.

114
00:05:18,079 --> 00:05:22,410
There's another method which is the file dot read lines.

115
00:05:23,420 --> 00:05:27,690
What this does is that it's gonna read all lines at once and will return

116
00:05:27,700 --> 00:05:33,119
them as a list of strings where each element is a line from the file.

117
00:05:33,320 --> 00:05:38,600
In other words, it combines the benefits of both your read and your read line

118
00:05:38,829 --> 00:05:43,429
by reading the whole file at once, just like the file dot read.

119
00:05:43,589 --> 00:05:44,529
However,

120
00:05:44,649 --> 00:05:49,559
it's gonna store each line as an individual element in a list.

121
00:05:50,040 --> 00:05:53,350
So let me give you an example. I'm going to go back in here.

122
00:05:53,459 --> 00:05:58,079
I'm going to modify this to change line to lines

123
00:05:58,510 --> 00:06:01,980
equals file dot read lines.

124
00:06:02,279 --> 00:06:03,000
OK?

125
00:06:03,260 --> 00:06:05,730
And now instead of using the while loop,

126
00:06:05,739 --> 00:06:10,760
I'm going to switch this over to the for loop and I'm gonna say four

127
00:06:10,890 --> 00:06:11,839
line

128
00:06:12,089 --> 00:06:12,660
in

129
00:06:12,839 --> 00:06:14,260
lines,

130
00:06:14,600 --> 00:06:15,450
colon.

131
00:06:15,790 --> 00:06:18,089
And now we can simply print

132
00:06:18,359 --> 00:06:19,339
in brackets

133
00:06:20,019 --> 00:06:21,130
a line.

134
00:06:21,869 --> 00:06:22,420
OK.

135
00:06:22,730 --> 00:06:23,279
Or better yet,

136
00:06:23,290 --> 00:06:26,940
let's add the strip method in order to remove any unnecessary spacing.

137
00:06:27,420 --> 00:06:29,739
And there it is right. Now, if I was to run the program,

138
00:06:29,839 --> 00:06:30,579
there you go,

139
00:06:30,730 --> 00:06:33,149
Alex Bob Caroline, Dave and Fred,

140
00:06:33,369 --> 00:06:33,929
again,

141
00:06:33,940 --> 00:06:36,309
you're not going to notice the difference when

142
00:06:36,320 --> 00:06:38,529
you're dealing with a very small file.

143
00:06:39,190 --> 00:06:42,619
But once again, do keep in mind that if you're dealing with large files,

144
00:06:42,760 --> 00:06:46,690
you want to go with either the red line or the read

145
00:06:46,829 --> 00:06:47,769
lines.

146
00:06:47,940 --> 00:06:53,179
The last method I want to show you is going to be the four line

147
00:06:53,359 --> 00:06:54,529
in file.

148
00:06:54,619 --> 00:06:57,100
And the purpose here is to read the line to read

149
00:06:57,109 --> 00:07:02,170
the file line by line in a memory efficient way.

150
00:07:02,390 --> 00:07:03,929
So I'm gonna go back in here.

151
00:07:04,410 --> 00:07:06,529
All right, let me remove the lines.

152
00:07:07,660 --> 00:07:14,399
So we have with open example dot TXTR file. So now I will say for line

153
00:07:15,089 --> 00:07:17,000
in, I could say file,

154
00:07:18,049 --> 00:07:20,339
OK? For line in file,

155
00:07:20,540 --> 00:07:23,459
now simply print line dot strip. And then once

156
00:07:23,600 --> 00:07:24,589
you run the program,

157
00:07:24,779 --> 00:07:25,549
there you go.

158
00:07:26,440 --> 00:07:30,609
This approach is actually very similar to using your read line,

159
00:07:30,720 --> 00:07:32,799
but it's actually more concise

160
00:07:32,929 --> 00:07:37,459
and considered the best practice for reading large

161
00:07:37,470 --> 00:07:40,619
files because each iteration retrieves a single line.

162
00:07:40,660 --> 00:07:43,529
So memory usage is going to be low.

163
00:07:43,750 --> 00:07:45,059
So to summarize

164
00:07:45,470 --> 00:07:50,100
if you want to read from a small file. The usual are read

165
00:07:50,829 --> 00:07:52,559
our file dot We will be

166
00:07:52,820 --> 00:07:53,589
the best.

167
00:07:53,859 --> 00:07:56,399
If you're reading from a large file,

168
00:07:56,549 --> 00:07:59,970
you can go with your line in file.

169
00:08:00,049 --> 00:08:04,429
It's better than your file dot Readline and also better than your file dot read

170
00:08:04,570 --> 00:08:05,690
lines.

171
00:08:05,859 --> 00:08:08,480
Now, I also just want to point out that

172
00:08:08,924 --> 00:08:15,375
in addition to the modes for your read mode, your append mode, your write mode,

173
00:08:15,445 --> 00:08:19,095
we also have additional modes like your read and write mode,

174
00:08:19,105 --> 00:08:23,584
your write and read mode as well as your append and read mode.

175
00:08:23,804 --> 00:08:25,024
As an example.

176
00:08:25,265 --> 00:08:26,445
Instead of R

177
00:08:26,644 --> 00:08:29,464
here, I can say R and then plus

178
00:08:29,869 --> 00:08:33,479
this is what we refer to as the read and write mode.

179
00:08:33,630 --> 00:08:37,960
So this is going to open up a file for both reading and writing.

180
00:08:37,969 --> 00:08:40,520
Keep in mind though that the file must exist

181
00:08:40,808 --> 00:08:45,549
and content can be read and modified. All right. So your file must already exist

182
00:08:45,950 --> 00:08:48,960
in order for you to be able to use this particular uh mode,

183
00:08:49,169 --> 00:08:51,950
we also have the write and

184
00:08:52,099 --> 00:08:52,750
read mode,

185
00:08:52,760 --> 00:08:55,530
which is going to be W and then plus this

186
00:08:55,539 --> 00:08:58,510
right here will open a file for reading and writing,

187
00:08:58,750 --> 00:09:02,590
but it was gonna replace any existing content.

188
00:09:02,840 --> 00:09:06,289
And then finally, of course, you have your append plus uh mode,

189
00:09:06,299 --> 00:09:08,090
which is the append and read mode.

190
00:09:08,210 --> 00:09:10,179
This will open up a file for reading, writing,

191
00:09:10,489 --> 00:09:12,380
appending new content at the end

192
00:09:12,530 --> 00:09:15,929
without changing any existing content. So

193
00:09:16,059 --> 00:09:17,979
you're more than Welcome to play around with these

194
00:09:17,989 --> 00:09:22,210
modes and see how they modify your file.

195
00:09:22,229 --> 00:09:24,700
Thank you for watching. I'm gonna see you in the next class.

