1
00:00:00,000 --> 00:00:00,000
Hello guys.

2
00:00:00,000 --> 00:00:03,000
So we are going to continue the discussion with respect to Python.

3
00:00:03,000 --> 00:00:06,000
And in this video we are going to talk about file operations.

4
00:00:06,000 --> 00:00:10,000
Uh, we will be performing various file operations with the help of Python programming language.

5
00:00:10,000 --> 00:00:14,000
Uh, we will be playing with both text and binary files.

6
00:00:14,000 --> 00:00:20,000
And we will also be understanding how to properly create files, how to write inside that, how to read

7
00:00:20,000 --> 00:00:22,000
the file content and many more things.

8
00:00:22,000 --> 00:00:23,000
So let us go ahead.

9
00:00:23,000 --> 00:00:29,000
And first of all, let me just go ahead and do the first task that is read a whole file.

10
00:00:29,000 --> 00:00:30,000
Right.

11
00:00:30,000 --> 00:00:35,000
So for reading a whole file again we will be using with open method.

12
00:00:35,000 --> 00:00:36,000
Right.

13
00:00:36,000 --> 00:00:38,000
And here I'm going to give my file name.

14
00:00:38,000 --> 00:00:44,000
Let's say that I'm going to give example dot txt and the first second parameter inside this open you

15
00:00:44,000 --> 00:00:49,000
know we will specifically use this are parameter are basically means are read okay.

16
00:00:49,000 --> 00:00:55,000
And then also other parameters which we will discuss like write uh write append, read append append

17
00:00:55,000 --> 00:00:55,000
write.

18
00:00:55,000 --> 00:00:56,000
Sorry not read append.

19
00:00:56,000 --> 00:00:58,000
It is append separately.

20
00:00:58,000 --> 00:01:03,000
Then we have w plus operation where we can both read and write a specific file.

21
00:01:03,000 --> 00:01:04,000
So we'll be seeing one by one.

22
00:01:04,000 --> 00:01:06,000
So let us first of all start with read.

23
00:01:06,000 --> 00:01:08,000
So this will be my read operation.

24
00:01:08,000 --> 00:01:10,000
And I'll create a object of file.

25
00:01:10,000 --> 00:01:14,000
And then from line in line okay.

26
00:01:14,000 --> 00:01:15,000
So from line in file.

27
00:01:15,000 --> 00:01:21,000
So inside this particular file how many lines are there like in example dot txt will be keep on reading

28
00:01:21,000 --> 00:01:21,000
it.

29
00:01:21,000 --> 00:01:24,000
And now let me do one thing instead of writing.

30
00:01:24,000 --> 00:01:29,000
If instead of just going line to line initially, we'll just go ahead and create a variable called as

31
00:01:29,000 --> 00:01:31,000
content and we'll take this file.

32
00:01:31,000 --> 00:01:34,000
And then there is a operation which is called as dot read.

33
00:01:34,000 --> 00:01:39,000
So what dot read is basically going to do is that it is just going to read the entire content of the

34
00:01:39,000 --> 00:01:39,000
file.

35
00:01:39,000 --> 00:01:39,000
Okay.

36
00:01:39,000 --> 00:01:43,000
So right now I have not created the file that is example dot txt.

37
00:01:43,000 --> 00:01:45,000
So we will go ahead and execute it.

38
00:01:45,000 --> 00:01:51,000
So here you can see that I am getting an error saying that hey no such file or directory in example

39
00:01:51,000 --> 00:01:52,000
dot txt right.

40
00:01:52,000 --> 00:01:57,000
So this is the first error we usually get whenever we are directly trying to read.

41
00:01:57,000 --> 00:01:59,000
I'll be showing you other ways also.

42
00:01:59,000 --> 00:02:04,000
But there what will happen is that uh, if I'm trying to read it right, first of all it will create

43
00:02:04,000 --> 00:02:06,000
a file and then we will be able to read it.

44
00:02:06,000 --> 00:02:06,000
Okay.

45
00:02:06,000 --> 00:02:08,000
Now here what I'm going to do.

46
00:02:08,000 --> 00:02:13,000
So this is the file handling part I will go ahead and create one example dot txt file okay.

47
00:02:13,000 --> 00:02:17,000
And with respect to this example dot txt file I'll just go ahead and write.

48
00:02:17,000 --> 00:02:17,000
Hello.

49
00:02:18,000 --> 00:02:19,000
How are you.

50
00:02:19,000 --> 00:02:20,000
Okay.

51
00:02:20,000 --> 00:02:22,000
And I am good okay.

52
00:02:23,000 --> 00:02:27,000
Something like this crush is my name okay.

53
00:02:27,000 --> 00:02:29,000
And welcome to the course okay.

54
00:02:29,000 --> 00:02:31,000
Something like this.

55
00:02:31,000 --> 00:02:36,000
So now I'm going to perform all these things I'll be reading specifically from this example dot txt.

56
00:02:36,000 --> 00:02:39,000
And now let me just go ahead and execute it okay.

57
00:02:39,000 --> 00:02:41,000
You'll be able to see that.

58
00:02:41,000 --> 00:02:44,000
Now I'm reading the entire content right.

59
00:02:44,000 --> 00:02:49,000
So with example dot txt this is basically opening this particular file in the read mode okay.

60
00:02:49,000 --> 00:02:52,000
We will not be able to write if we are opening with the help of read mode.

61
00:02:52,000 --> 00:02:53,000
Right.

62
00:02:53,000 --> 00:02:55,000
And then we are just reading the entire file content.

63
00:02:55,000 --> 00:03:02,000
Now, I may also have a scenario that I want to read a file line by line.

64
00:03:02,000 --> 00:03:02,000
Okay.

65
00:03:02,000 --> 00:03:04,000
I just want to read it line by line.

66
00:03:04,000 --> 00:03:06,000
Now how do I read it line by line.

67
00:03:06,000 --> 00:03:12,000
So here we are going to specifically use with open operator right here I'm going to use my again example

68
00:03:12,000 --> 00:03:13,000
dot txt file.

69
00:03:13,000 --> 00:03:17,000
And this will specifically open in read mode.

70
00:03:17,000 --> 00:03:20,000
And now I'm going to use this particular object as file.

71
00:03:20,000 --> 00:03:23,000
Now since I have to read all these things line by line.

72
00:03:23,000 --> 00:03:30,000
So now I can go ahead and use my for loops, you know, so that file will be, uh, over here as an

73
00:03:30,000 --> 00:03:33,000
iterable because I have so much of content over here.

74
00:03:33,000 --> 00:03:33,000
Right.

75
00:03:33,000 --> 00:03:36,000
So for line in file, right.

76
00:03:36,000 --> 00:03:40,000
I'm just going to go ahead and print my line.

77
00:03:40,000 --> 00:03:40,000
Okay.

78
00:03:41,000 --> 00:03:45,000
And uh, what I'm actually also going to do is that if I go ahead and execute this.

79
00:03:45,000 --> 00:03:47,000
So here you can see example okay.

80
00:03:47,000 --> 00:03:49,000
Example spelling is not right.

81
00:03:49,000 --> 00:03:54,000
So I'll just make it right now here you can see that when I'm reading line by line it is also taking

82
00:03:54,000 --> 00:03:55,000
the new line characters.

83
00:03:55,000 --> 00:03:56,000
Right.

84
00:03:56,000 --> 00:04:00,000
So here because when I see this right if I go to the next line.

85
00:04:00,000 --> 00:04:02,000
So here a new line is basically getting added.

86
00:04:02,000 --> 00:04:04,000
So that is the reason you'll be able to see this space.

87
00:04:04,000 --> 00:04:07,000
Now let's say I want to go ahead and remove this particular new line character.

88
00:04:07,000 --> 00:04:11,000
So for that I'll be using an operation which is called as dot strip.

89
00:04:11,000 --> 00:04:11,000
Okay.

90
00:04:11,000 --> 00:04:20,000
So this dot strip, uh, this dot strip operation removes the new line character.

91
00:04:20,000 --> 00:04:21,000
Perfect.

92
00:04:21,000 --> 00:04:24,000
So this is what we are able to do it out of it okay.

93
00:04:24,000 --> 00:04:25,000
So this is what is there.

94
00:04:25,000 --> 00:04:26,000
Right.

95
00:04:26,000 --> 00:04:30,000
And we are able to get the output now, uh, let's do some more operation.

96
00:04:30,000 --> 00:04:36,000
Now what I'm actually going to do over here is that, uh, uh, let's say that I want to probably,

97
00:04:36,000 --> 00:04:40,000
uh, uh, open a file for writing something inside it.

98
00:04:40,000 --> 00:04:40,000
Right.

99
00:04:40,000 --> 00:04:44,000
So here, let me go ahead and write writing a file.

100
00:04:44,000 --> 00:04:45,000
Okay.

101
00:04:45,000 --> 00:04:49,000
And here I will also make sure that I will show you overwriting clauses.

102
00:04:49,000 --> 00:04:49,000
Okay.

103
00:04:49,000 --> 00:04:53,000
Over here right now let me do the same thing with open function.

104
00:04:53,000 --> 00:04:55,000
And I'm going to basically open this.

105
00:04:55,000 --> 00:04:59,000
And here I'm going to use example dot txt.

106
00:05:00,000 --> 00:05:00,000
Right.

107
00:05:00,000 --> 00:05:05,000
And this will basically be my right byte mode and as file okay.

108
00:05:05,000 --> 00:05:08,000
And then I will go ahead and write file dot right.

109
00:05:08,000 --> 00:05:12,000
And let me quickly go ahead and write Hello world.

110
00:05:12,000 --> 00:05:16,000
Okay I'm writing this Hello world with a new line character okay.

111
00:05:16,000 --> 00:05:22,000
So fi dot write right is a method which will specifically help me to write anything inside this file.

112
00:05:22,000 --> 00:05:25,000
And here you can see that I'm using W mode.

113
00:05:25,000 --> 00:05:27,000
Over here W mode is nothing but write mode okay.

114
00:05:28,000 --> 00:05:32,000
Now this is the first line that I'm going to add over there, uh, with a new line.

115
00:05:32,000 --> 00:05:35,000
And then I'm going to add file dot write.

116
00:05:35,000 --> 00:05:37,000
This is again a new line.

117
00:05:37,000 --> 00:05:39,000
So let's say I'll just go ahead and write.

118
00:05:39,000 --> 00:05:42,000
This is a new line okay.

119
00:05:42,000 --> 00:05:45,000
And let me just go ahead and execute this.

120
00:05:45,000 --> 00:05:51,000
So let's see, uh, what will basically happen now as soon as I execute this already, you know, in

121
00:05:51,000 --> 00:05:53,000
example dot txt I have this content.

122
00:05:53,000 --> 00:06:00,000
So if I go ahead and execute it here, you will be able to see that the entire content got overwritten.

123
00:06:00,000 --> 00:06:01,000
Right.

124
00:06:01,000 --> 00:06:06,000
So overwritten basically because I have just opened this particular file for the write mode and it will

125
00:06:06,000 --> 00:06:08,000
just consider, okay, the file is completely empty.

126
00:06:08,000 --> 00:06:12,000
And when we write file dot right, it is just going to overwrite completely from this particular content.

127
00:06:12,000 --> 00:06:15,000
And again this is a problem out there.

128
00:06:15,000 --> 00:06:16,000
You know we should never be doing like this.

129
00:06:16,000 --> 00:06:25,000
Instead uh, if I want to write a file, write a file, write write a file without overwriting, okay.

130
00:06:25,000 --> 00:06:30,000
Without overwriting, then I'll be using a different approach.

131
00:06:30,000 --> 00:06:32,000
So let me just go ahead and do it.

132
00:06:32,000 --> 00:06:34,000
So I'll say from open.

133
00:06:35,000 --> 00:06:35,000
Okay.

134
00:06:35,000 --> 00:06:39,000
And here I'm going to specifically use example dot txt.

135
00:06:39,000 --> 00:06:44,000
And let me just open this in another mode which is called as append mode okay.

136
00:06:44,000 --> 00:06:48,000
Now in this append mode what will happen is that if I go ahead and write anything.

137
00:06:48,000 --> 00:06:54,000
So if I go ahead and write file dot write uh append operation taking place.

138
00:06:55,000 --> 00:06:57,000
Append operation taking place.

139
00:06:57,000 --> 00:06:58,000
Okay.

140
00:06:58,000 --> 00:07:00,000
And if I go ahead and execute it now.

141
00:07:00,000 --> 00:07:03,000
So here you'll be able to see this operation has taken place.

142
00:07:03,000 --> 00:07:09,000
Now, if I go ahead and see in the example dot txt, this is the new thing that is getting appended,

143
00:07:09,000 --> 00:07:09,000
right?

144
00:07:10,000 --> 00:07:12,000
I may also want to write it in the next line.

145
00:07:12,000 --> 00:07:15,000
So you can just use slash n over here and do it right.

146
00:07:15,000 --> 00:07:19,000
And now here clearly you can see that I'm able to append any text.

147
00:07:19,000 --> 00:07:20,000
Okay.

148
00:07:21,000 --> 00:07:23,000
Let's say I will go ahead and write slash.

149
00:07:23,000 --> 00:07:24,000
And again I'll try to append this.

150
00:07:25,000 --> 00:07:25,000
Okay.

151
00:07:25,000 --> 00:07:27,000
So here you'll be able to see again it is getting appended.

152
00:07:27,000 --> 00:07:32,000
Now along with that what I'm actually going to do is that again let's go ahead and append this.

153
00:07:32,000 --> 00:07:36,000
So here you can see it will go to the new line because I had used slash n before.

154
00:07:36,000 --> 00:07:37,000
Right.

155
00:07:37,000 --> 00:07:39,000
So the file operation is basically taking place.

156
00:07:39,000 --> 00:07:40,000
And here you are able to append it.

157
00:07:40,000 --> 00:07:41,000
Okay.

158
00:07:41,000 --> 00:07:45,000
Uh now uh there may be also scenarios that I want to write.

159
00:07:45,000 --> 00:07:47,000
Write multiple lines okay.

160
00:07:47,000 --> 00:07:49,000
Multiple lines specifically.

161
00:07:49,000 --> 00:07:55,000
So are writing a list of lines to a file.

162
00:07:55,000 --> 00:07:56,000
So let's do this okay.

163
00:07:56,000 --> 00:07:59,000
Again uh let's say this is lines okay.

164
00:07:59,000 --> 00:08:01,000
These are my list of lines.

165
00:08:01,000 --> 00:08:02,000
So here I'm just going to write okay.

166
00:08:02,000 --> 00:08:12,000
This is my first line slash n uh then the second line slash n okay.

167
00:08:12,000 --> 00:08:14,000
Um, and let's go to the third line.

168
00:08:15,000 --> 00:08:16,000
Third line.

169
00:08:18,000 --> 00:08:19,000
Slash n okay.

170
00:08:19,000 --> 00:08:23,000
So these are my three lines that I really want to add.

171
00:08:23,000 --> 00:08:23,000
Okay.

172
00:08:24,000 --> 00:08:28,000
Uh, let me make some cells and let me copy this thing entirely.

173
00:08:28,000 --> 00:08:29,000
Okay.

174
00:08:29,000 --> 00:08:31,000
And let me do this operation over here.

175
00:08:31,000 --> 00:08:32,000
Okay.

176
00:08:32,000 --> 00:08:36,000
Now what I'm actually going to do, I will just go ahead and open my file.

177
00:08:36,000 --> 00:08:38,000
Open my file.

178
00:08:38,000 --> 00:08:38,000
Okay.

179
00:08:38,000 --> 00:08:45,000
And I will try to open this same example dot txt file in my append mode okay.

180
00:08:45,000 --> 00:08:47,000
In my append mode.

181
00:08:47,000 --> 00:08:49,000
And I'll create an object as file.

182
00:08:49,000 --> 00:08:54,000
Now in order to write all the lines right inside this particular list, there is again a function which

183
00:08:54,000 --> 00:08:59,000
I can actually use, which is called as file dot write lines and all.

184
00:08:59,000 --> 00:09:01,000
I have to give my list over here, right?

185
00:09:01,000 --> 00:09:03,000
So once I execute it you will be able to see that.

186
00:09:03,000 --> 00:09:07,000
Now first line, second line and third line has got appended.

187
00:09:07,000 --> 00:09:07,000
Okay.

188
00:09:07,000 --> 00:09:11,000
So uh apologies for this particular spelling kind of errors.

189
00:09:11,000 --> 00:09:16,000
So you can actually make the changes as based on your grammatical things that you have.

190
00:09:16,000 --> 00:09:17,000
Right.

191
00:09:17,000 --> 00:09:23,000
So I'm just making sure that I try to show you more amazing features with respect to this.

192
00:09:23,000 --> 00:09:23,000
Okay.

193
00:09:23,000 --> 00:09:25,000
Now this was with respect to text file.

194
00:09:25,000 --> 00:09:29,000
Now let's go ahead and discuss with respect to binary files okay.

195
00:09:29,000 --> 00:09:30,000
How to work with binary file.

196
00:09:31,000 --> 00:09:38,000
Binary files are usually uh if I probably see the extension it is basically a bin file.

197
00:09:38,000 --> 00:09:38,000
Right.

198
00:09:38,000 --> 00:09:44,000
So uh, if I go ahead and create a file over here, you'll be able to see if I go and write example

199
00:09:44,000 --> 00:09:44,000
dot bin okay.

200
00:09:44,000 --> 00:09:48,000
So this is what we basically say it as binary files okay.

201
00:09:48,000 --> 00:09:51,000
Let me just go ahead and append something over here.

202
00:09:51,000 --> 00:09:52,000
Hello world.

203
00:09:52,000 --> 00:09:52,000
Something okay.

204
00:09:52,000 --> 00:09:54,000
Some information over here.

205
00:09:54,000 --> 00:09:56,000
And this will be in the form of binary files.

206
00:09:56,000 --> 00:10:02,000
Now I'll go ahead and show you how you can read or write binary files.

207
00:10:02,000 --> 00:10:05,000
In binary files we usually update bytes okay.

208
00:10:05,000 --> 00:10:06,000
Bytes is very much important.

209
00:10:06,000 --> 00:10:10,000
So I'll just keep this as empty right now with respect to binary files.

210
00:10:10,000 --> 00:10:12,000
So let me just go ahead and write.

211
00:10:12,000 --> 00:10:16,000
So here I'm just going to write to a binary file.

212
00:10:16,000 --> 00:10:18,000
I'll be using this kind of bytes okay.

213
00:10:18,000 --> 00:10:22,000
Uh with open example dot bin in right byte mode okay.

214
00:10:22,000 --> 00:10:28,000
So now my uh this operation will become it will not be read or write, it will be read byte or write

215
00:10:28,000 --> 00:10:29,000
byte.

216
00:10:29,000 --> 00:10:29,000
Okay.

217
00:10:29,000 --> 00:10:30,000
Since I am going to write.

218
00:10:30,000 --> 00:10:32,000
So I'm going to basically use it as write byte.

219
00:10:32,000 --> 00:10:34,000
Now if I go ahead and execute it.

220
00:10:34,000 --> 00:10:38,000
So here you'll be able to see in the bin this is what I am actually able to get okay.

221
00:10:38,000 --> 00:10:40,000
In in the form of bytes okay.

222
00:10:40,000 --> 00:10:43,000
So this is one of the operation that you can see over here right.

223
00:10:43,000 --> 00:10:49,000
Similarly if you want to probably read it uh, again we can use instead of writing.

224
00:10:49,000 --> 00:10:52,000
Ah we can also use RB okay.

225
00:10:52,000 --> 00:10:54,000
So RB as file file dot read.

226
00:10:54,000 --> 00:10:56,000
And we are just going to print the content.

227
00:10:56,000 --> 00:10:59,000
And this is what I am able to display right now in bin file.

228
00:10:59,000 --> 00:11:00,000
It is looking like this.

229
00:11:00,000 --> 00:11:02,000
And this is what I think.

230
00:11:02,000 --> 00:11:06,000
Uh, actually, uh, hex characters for the same thing, what we have actually written.

231
00:11:06,000 --> 00:11:07,000
Okay.

232
00:11:07,000 --> 00:11:12,000
So this was one of the most, uh, beautiful thing with respect to file operation.

233
00:11:12,000 --> 00:11:19,000
Now, as I said, uh, let's do some of the things, write some, some, some practical examples and

234
00:11:19,000 --> 00:11:21,000
all, uh, how to probably solve it.

235
00:11:21,000 --> 00:11:22,000
Okay.

236
00:11:22,000 --> 00:11:25,000
Uh, let's say, uh, I want to read.

237
00:11:26,000 --> 00:11:38,000
I want to read the content from a source text file and write it to a destination text file.

238
00:11:38,000 --> 00:11:39,000
Okay.

239
00:11:39,000 --> 00:11:42,000
If I want to do this, I can go ahead and write it again.

240
00:11:42,000 --> 00:11:44,000
For this, it is very much simple.

241
00:11:44,000 --> 00:11:46,000
I will first of all read from a file.

242
00:11:46,000 --> 00:11:53,000
So let's say I am reading from my example dot txt and I'm writing it to a destination dot txt.

243
00:11:54,000 --> 00:11:56,000
So I have not created destination dot txt.

244
00:11:56,000 --> 00:11:58,000
Let's see whether we will get an error or not.

245
00:11:58,000 --> 00:12:00,000
So here is the content that I am getting.

246
00:12:00,000 --> 00:12:01,000
Source underscore file dot read.

247
00:12:01,000 --> 00:12:05,000
And when I write destination underscore file dot write content okay.

248
00:12:05,000 --> 00:12:10,000
So if I execute it here automatically you'll be able to see that I'll be creating this destination dot

249
00:12:10,000 --> 00:12:11,000
txt.

250
00:12:11,000 --> 00:12:15,000
And this was my example dot txt okay this is one of the operation.

251
00:12:15,000 --> 00:12:19,000
One more uh assignment that you can probably take.

252
00:12:19,000 --> 00:12:21,000
Again, please make sure that you do this.

253
00:12:21,000 --> 00:12:22,000
Uh, it will be important.

254
00:12:22,000 --> 00:12:23,000
Okay.

255
00:12:23,000 --> 00:12:26,000
Um, and, uh, read read a text file.

256
00:12:26,000 --> 00:12:26,000
Okay.

257
00:12:26,000 --> 00:12:31,000
And I have already done this, I think, for you all, but I'll give you this as an assignment.

258
00:12:31,000 --> 00:12:34,000
Read a text file and count the number of lines, words and characters.

259
00:12:34,000 --> 00:12:36,000
And this is the code.

260
00:12:36,000 --> 00:12:37,000
So first of all, you try it from your side.

261
00:12:37,000 --> 00:12:39,000
Anyhow, I'll be giving you the code.

262
00:12:39,000 --> 00:12:40,000
Okay.

263
00:12:40,000 --> 00:12:43,000
You can give the file part example dot txt and just go ahead and do this right.

264
00:12:43,000 --> 00:12:45,000
So this is a function that is got created.

265
00:12:45,000 --> 00:12:47,000
We are reading the lines we are.

266
00:12:47,000 --> 00:12:50,000
We are doing the summing up of all the lines itself okay.

267
00:12:50,000 --> 00:12:52,000
So this one assignment you can probably take it okay.

268
00:12:53,000 --> 00:13:00,000
So uh I think uh we have discussed so many things about it, but uh, let us go ahead and discuss about

269
00:13:00,000 --> 00:13:01,000
one more operation.

270
00:13:01,000 --> 00:13:02,000
Okay.

271
00:13:02,000 --> 00:13:09,000
Uh, that is nothing but writing and reading.

272
00:13:10,000 --> 00:13:12,000
Writing and then reading a file.

273
00:13:12,000 --> 00:13:12,000
Okay.

274
00:13:12,000 --> 00:13:14,000
This kind of operation.

275
00:13:14,000 --> 00:13:20,000
Okay, so what I'm actually going to do, I'm going to basically use with open again and let me just

276
00:13:20,000 --> 00:13:24,000
go ahead and write example dot txt okay.

277
00:13:24,000 --> 00:13:28,000
And this time I'm going to open this file in W plus mode.

278
00:13:28,000 --> 00:13:30,000
W plus mode is nothing but a.

279
00:13:30,000 --> 00:13:35,000
If you don't know about W plus mode, I'll just give a statement over here so that you'll be having

280
00:13:35,000 --> 00:13:36,000
this as a markdown.

281
00:13:37,000 --> 00:13:42,000
The W plus mode in Python is used to open a file for both reading and writing.

282
00:13:42,000 --> 00:13:44,000
If the file does not exist, it will be created.

283
00:13:44,000 --> 00:13:47,000
If the file exists, the cut its content is truncated.

284
00:13:47,000 --> 00:13:49,000
That is, the file is overwritten.

285
00:13:49,000 --> 00:13:53,000
Okay, so here you will be able to see that I'm using W plus mode uh as file.

286
00:13:53,000 --> 00:13:54,000
Okay.

287
00:13:54,000 --> 00:13:59,000
And here I'm just going to go ahead and uh probably write the data to the file.

288
00:13:59,000 --> 00:14:01,000
So let me just go ahead and write file dot write.

289
00:14:02,000 --> 00:14:06,000
And here I'm just going to say hey hello world okay.

290
00:14:06,000 --> 00:14:09,000
It is just going to overwrite it okay.

291
00:14:09,000 --> 00:14:14,000
And then I will go ahead and write file dot write okay.

292
00:14:14,000 --> 00:14:17,000
This is a new line.

293
00:14:17,000 --> 00:14:18,000
Okay.

294
00:14:18,000 --> 00:14:19,000
This is done.

295
00:14:19,000 --> 00:14:20,000
Okay, perfect.

296
00:14:20,000 --> 00:14:26,000
Now, after we write, uh, we will make sure that we move the file.

297
00:14:26,000 --> 00:14:27,000
Cursor.

298
00:14:27,000 --> 00:14:27,000
Okay.

299
00:14:27,000 --> 00:14:27,000
What?

300
00:14:27,000 --> 00:14:30,000
Right now, the file cursor will be pointing to the second line.

301
00:14:30,000 --> 00:14:31,000
Okay.

302
00:14:31,000 --> 00:14:34,000
It will be pointing to the second line that is over here.

303
00:14:34,000 --> 00:14:38,000
But we need to move the cursor to the first one so that we will be able to read it.

304
00:14:38,000 --> 00:14:38,000
Okay.

305
00:14:38,000 --> 00:14:41,000
So move the file cursor to the beginning.

306
00:14:41,000 --> 00:14:42,000
And how do we do this?

307
00:14:42,000 --> 00:14:48,000
We just use one very important method which is called as file dot seek.

308
00:14:48,000 --> 00:14:49,000
Okay.

309
00:14:49,000 --> 00:14:56,000
Now this seek uh, if you probably go ahead and see this this if I give it to zero.

310
00:14:56,000 --> 00:14:59,000
So it is just going to move the file cursor to the beginning.

311
00:14:59,000 --> 00:15:02,000
That is just like a zeroth character or zeroth index.

312
00:15:02,000 --> 00:15:02,000
Right.

313
00:15:02,000 --> 00:15:10,000
Then we can go ahead and read the content of the file.

314
00:15:11,000 --> 00:15:12,000
Right.

315
00:15:12,000 --> 00:15:14,000
And then I'll go ahead and print the content.

316
00:15:14,000 --> 00:15:15,000
Okay.

317
00:15:15,000 --> 00:15:19,000
First of all, I'll show you what will happen if I commented this out okay.

318
00:15:19,000 --> 00:15:21,000
Will it display anything okay.

319
00:15:21,000 --> 00:15:22,000
So let's go ahead and run this.

320
00:15:22,000 --> 00:15:24,000
So here you can see I'm getting hello world.

321
00:15:24,000 --> 00:15:27,000
Uh this is a new text line.

322
00:15:27,000 --> 00:15:28,000
Uh, okay.

323
00:15:28,000 --> 00:15:30,000
Uh, let's see what is getting printed.

324
00:15:30,000 --> 00:15:32,000
So print content is basically happening over here.

325
00:15:33,000 --> 00:15:37,000
Uh, let me see whether my example dot txt has got loaded.

326
00:15:37,000 --> 00:15:43,000
So example dot txt this overloaded uh this is basically giving me a new sentence hello world and all.

327
00:15:43,000 --> 00:15:45,000
And here you will be able to see that I'm printing the content.

328
00:15:45,000 --> 00:15:46,000
Okay.

329
00:15:46,000 --> 00:15:47,000
Uh, just a second.

330
00:15:47,000 --> 00:15:47,000
Okay.

331
00:15:47,000 --> 00:15:52,000
So the thing is that I did not read the file, so previous variable only it was showing me.

332
00:15:52,000 --> 00:15:54,000
Now, let me do one thing.

333
00:15:54,000 --> 00:15:54,000
Okay.

334
00:15:55,000 --> 00:15:56,000
File dot read.

335
00:15:56,000 --> 00:15:56,000
Okay.

336
00:15:56,000 --> 00:15:58,000
Now I did not read the file.

337
00:15:58,000 --> 00:15:58,000
Right.

338
00:15:58,000 --> 00:15:59,000
So what I'm actually going to do.

339
00:15:59,000 --> 00:16:02,000
See without I'm just commenting this okay.

340
00:16:02,000 --> 00:16:07,000
And we are writing this two lines in my example dot txt and we are reading it.

341
00:16:07,000 --> 00:16:10,000
If we do not write this line of code, what will happen?

342
00:16:10,000 --> 00:16:11,000
It will not display us anything.

343
00:16:12,000 --> 00:16:12,000
Okay.

344
00:16:12,000 --> 00:16:17,000
And here you can see in example dot txt this two line has got appended because the cursor will be pointing

345
00:16:17,000 --> 00:16:17,000
here.

346
00:16:17,000 --> 00:16:22,000
We have to make sure that we have to take the cursor over here right to the first one.

347
00:16:22,000 --> 00:16:24,000
So that is the reason you will be able to see once.

348
00:16:24,000 --> 00:16:28,000
I probably remove this and if I execute it now, you will be able to see that I am actually able to

349
00:16:28,000 --> 00:16:30,000
get the both the line.

350
00:16:30,000 --> 00:16:36,000
Okay, so, uh, this is what, uh, you'll be able to see with respect to this and, uh, how amazing

351
00:16:36,000 --> 00:16:37,000
this is, right?

352
00:16:37,000 --> 00:16:42,000
Uh, again, uh, let's say that you want to do multiple times.

353
00:16:42,000 --> 00:16:43,000
Read and write.

354
00:16:43,000 --> 00:16:47,000
Always make sure that you try to use this file dot, seek and make it to the beginning one.

355
00:16:47,000 --> 00:16:48,000
Right.

356
00:16:49,000 --> 00:16:50,000
Um, yes.

357
00:16:50,000 --> 00:16:51,000
Uh, this was it.

358
00:16:51,000 --> 00:16:56,000
Uh, I think I had, uh, probably taken a lot many things with respect to file handling.

359
00:16:56,000 --> 00:17:01,000
Uh, but yes, as we go ahead and see multiple use cases as we go ahead in the upcoming sessions, we'll

360
00:17:01,000 --> 00:17:03,000
be seeing more different kind of operations.

361
00:17:03,000 --> 00:17:05,000
So yes, this was it for my side.

362
00:17:05,000 --> 00:17:07,000
I will see you all in the next video.

363
00:17:07,000 --> 00:17:07,000
Thank you.

364
00:17:07,000 --> 00:17:07,000
Take care.

