1
00:00:00,009 --> 00:00:05,510
All right. So let's talk about file handling. And right now as it is

2
00:00:05,809 --> 00:00:11,819
the only file we have been working with is the main dot py file.

3
00:00:12,050 --> 00:00:14,590
It's just one Python file and nothing more.

4
00:00:14,939 --> 00:00:15,930
However,

5
00:00:16,090 --> 00:00:19,209
we could create other types of files.

6
00:00:19,280 --> 00:00:22,209
What if we wanted to write a program that would

7
00:00:22,219 --> 00:00:25,750
ask the user to provide their username or password?

8
00:00:26,059 --> 00:00:27,319
And then we store

9
00:00:27,440 --> 00:00:32,439
that information, that user information in a separate file.

10
00:00:32,650 --> 00:00:34,880
How can we do that? But we can do that

11
00:00:35,279 --> 00:00:37,720
in a variety of ways. And that's what you're going to learn here. So

12
00:00:38,259 --> 00:00:42,479
the first thing I want to show you is how to create a file.

13
00:00:43,250 --> 00:00:46,360
There is a statement called with

14
00:00:47,479 --> 00:00:51,299
and then a function called open,

15
00:00:52,080 --> 00:00:52,619
all right

16
00:00:53,400 --> 00:00:54,360
brackets.

17
00:00:54,750 --> 00:01:00,810
What this will do is that the open function typically accepts two parameters. All

18
00:01:01,560 --> 00:01:04,339
you will have to provide the name of the file

19
00:01:04,569 --> 00:01:05,989
that you want to work with.

20
00:01:06,300 --> 00:01:08,559
And then the second parameter will be

21
00:01:08,669 --> 00:01:12,800
whatever it is that you want to do to that file, maybe you want to read to the file,

22
00:01:12,809 --> 00:01:15,220
write to the file, append the file and so on.

23
00:01:15,489 --> 00:01:17,989
So as an example, right? Let me say

24
00:01:18,529 --> 00:01:21,349
I want to open up a file called example,

25
00:01:22,330 --> 00:01:25,980
OK dot TXT. So it's a text file, right?

26
00:01:26,480 --> 00:01:27,910
And now comma

27
00:01:28,370 --> 00:01:31,029
and now I have to provide

28
00:01:31,400 --> 00:01:36,069
the operation. Do I want to open this file? Do I want to read the contents of the file?

29
00:01:36,080 --> 00:01:36,870
What do I want to do?

30
00:01:37,150 --> 00:01:40,510
I want to write to the file. So I'm going to use W

31
00:01:40,620 --> 00:01:44,639
to represent the right function, the right operation.

32
00:01:45,160 --> 00:01:47,400
And now to finish this, I'm gonna say

33
00:01:47,629 --> 00:01:48,910
file.

34
00:01:49,290 --> 00:01:50,709
So this right here,

35
00:01:51,379 --> 00:01:54,000
this is typically the syntax, OK? You will say with

36
00:01:54,370 --> 00:01:57,349
open and then in brackets provide the name of the file

37
00:01:57,550 --> 00:02:04,029
and then the operator, whether it's W for write R for read and so on and then say S file.

38
00:02:04,389 --> 00:02:10,240
Now I'm gonna jump over to the new line and say file dot write.

39
00:02:10,529 --> 00:02:14,119
This is going to be the function that we're going to use

40
00:02:14,250 --> 00:02:15,490
to actually

41
00:02:15,690 --> 00:02:19,369
write the string or whatever it is that we want to write to the file. So

42
00:02:19,589 --> 00:02:20,679
in brackets,

43
00:02:21,020 --> 00:02:22,119
I'm gonna say

44
00:02:22,440 --> 00:02:24,039
hello world,

45
00:02:24,539 --> 00:02:27,110
this is what I want to write to the file.

46
00:02:27,250 --> 00:02:28,000
Now,

47
00:02:28,149 --> 00:02:29,960
check this out.

48
00:02:30,179 --> 00:02:35,039
I'm gonna go ahead now notice so far, I still have only one file main dot py.

49
00:02:35,529 --> 00:02:38,000
But if I was to run the program,

50
00:02:38,690 --> 00:02:40,029
do you now see

51
00:02:40,320 --> 00:02:43,610
it says example dot txt.

52
00:02:43,800 --> 00:02:48,470
So even though I didn't, didn't have this file created before,

53
00:02:48,880 --> 00:02:51,309
whenever you're writing to a file,

54
00:02:51,820 --> 00:02:54,990
if that file hasn't existed before.

55
00:02:55,309 --> 00:03:00,110
Then Python will automatically create that file for you.

56
00:03:01,199 --> 00:03:01,729
Now,

57
00:03:02,380 --> 00:03:07,110
if I was to open up my example dot text, you can see right now it says hello

58
00:03:07,259 --> 00:03:08,029
World.

59
00:03:08,580 --> 00:03:09,600
What if

60
00:03:10,119 --> 00:03:13,320
I come over here right now and I write another

61
00:03:13,750 --> 00:03:14,839
right function?

62
00:03:15,580 --> 00:03:16,639
And I say

63
00:03:17,100 --> 00:03:19,460
this is awesome.

64
00:03:19,979 --> 00:03:23,529
What do you think is gonna happen? Let's go ahead and run the program.

65
00:03:23,839 --> 00:03:29,699
If I go back to my example Doc TXT now you can see it says hello world. This is awesome.

66
00:03:29,710 --> 00:03:31,350
So now it has added

67
00:03:31,600 --> 00:03:38,119
the, this is awesome to our file. Let me go back and close this. Let me remove

68
00:03:38,539 --> 00:03:40,279
the second file function.

69
00:03:40,960 --> 00:03:41,490
All right,

70
00:03:41,779 --> 00:03:42,419
now

71
00:03:43,220 --> 00:03:47,300
we can see that hello world is in fact an example dot TXT.

72
00:03:47,539 --> 00:03:50,520
But what if in our console, we wanted to display

73
00:03:50,720 --> 00:03:54,080
the contents of example dot TXT.

74
00:03:54,360 --> 00:03:57,800
What I will do here is I'll say with open again.

75
00:03:58,490 --> 00:03:59,190
And now

76
00:03:59,529 --> 00:04:03,759
in brackets once again the name of the file example dot TXT.

77
00:04:04,229 --> 00:04:09,229
And now we're going to use the R function instead.

78
00:04:09,570 --> 00:04:13,350
This is what we use for reading the file. So

79
00:04:13,750 --> 00:04:16,350
I'm gonna say as file colon.

80
00:04:17,058 --> 00:04:17,829
And now

81
00:04:18,088 --> 00:04:22,479
what if we wanted to print out the contents of the file?

82
00:04:23,760 --> 00:04:27,179
I can assign a variable called content

83
00:04:28,450 --> 00:04:29,619
equals.

84
00:04:29,959 --> 00:04:30,959
And now

85
00:04:31,130 --> 00:04:34,329
I'm going to use the function file dot Read

86
00:04:34,709 --> 00:04:36,899
this function right here. File dot We

87
00:04:37,209 --> 00:04:40,459
will read the contents of our file.

88
00:04:40,579 --> 00:04:43,299
It's going to pass the contents to the variable content.

89
00:04:43,500 --> 00:04:48,010
And now all we need to do from here will be to simply say prints and then in brackets,

90
00:04:48,019 --> 00:04:49,929
what are we printing, we're printing content.

91
00:04:51,390 --> 00:04:52,010
OK?

92
00:04:52,200 --> 00:04:53,690
Now I'm gonna run

93
00:04:54,880 --> 00:04:56,880
and there it is right here in the console,

94
00:04:56,890 --> 00:05:01,619
you can see we have hello world being displayed.

95
00:05:01,980 --> 00:05:02,540
So

96
00:05:02,679 --> 00:05:03,500
this

97
00:05:03,660 --> 00:05:06,600
is basically an introduction to

98
00:05:06,839 --> 00:05:09,869
reading from files, writing to files.

99
00:05:10,000 --> 00:05:13,679
And one question you may have is OK,

100
00:05:13,859 --> 00:05:17,079
can we actually create our files

101
00:05:17,200 --> 00:05:21,179
without using the with statement?

102
00:05:21,309 --> 00:05:26,149
And the answer in fact is yes, we can do so without using the with statement. So

103
00:05:26,279 --> 00:05:28,250
how would we do this? Well,

104
00:05:29,700 --> 00:05:32,959
let me just, first of all, remove the program in here. What we need to do.

105
00:05:32,970 --> 00:05:34,320
First of all is to

106
00:05:34,619 --> 00:05:35,760
say file

107
00:05:36,600 --> 00:05:38,619
equals and then open,

108
00:05:39,130 --> 00:05:41,299
this is going to be the actual open function

109
00:05:41,309 --> 00:05:44,970
itself and remember that open accepts two parameters.

110
00:05:44,980 --> 00:05:45,570
So now

111
00:05:45,799 --> 00:05:48,540
we'll have to open up our example

112
00:05:48,660 --> 00:05:49,769
dot TXT.

113
00:05:50,600 --> 00:05:56,179
And now what I want to do, we want to write to the file. So I'm gonna say w

114
00:05:56,489 --> 00:06:00,779
let me just remove, let me delete this example dot text file. OK? Let me delete it.

115
00:06:01,559 --> 00:06:04,410
OK? So like we're starting from the very scratch, right?

116
00:06:04,609 --> 00:06:05,299
So now

117
00:06:05,649 --> 00:06:10,820
I have told Python, OK? I want you to create this file called example dot TXT.

118
00:06:11,239 --> 00:06:14,149
What do we want to now write to the file?

119
00:06:14,190 --> 00:06:17,579
I'm gonna come in right now and say file dot write,

120
00:06:17,790 --> 00:06:17,970
OK?

121
00:06:17,980 --> 00:06:22,690
Because now I'm writing to the file and then in brackets, let's provide the string.

122
00:06:23,100 --> 00:06:24,820
Hello world.

123
00:06:25,630 --> 00:06:28,429
And now what I would have to do

124
00:06:28,679 --> 00:06:32,959
is I'm gonna have to close the file when you're opening

125
00:06:32,970 --> 00:06:35,690
up a file or you're waiting to a file or you're

126
00:06:35,829 --> 00:06:36,760
into a file

127
00:06:37,010 --> 00:06:40,769
by default, the file needs to be opened in order for that operation to occur.

128
00:06:41,029 --> 00:06:44,369
What you always want to do is you want to ensure that the file is cool.

129
00:06:44,904 --> 00:06:48,255
Once the operation has finished, if you don't do this,

130
00:06:48,355 --> 00:06:49,945
this can lead to programs

131
00:06:50,045 --> 00:06:56,174
leaking out memory and it can be very vulnerable to different types of attacks. So

132
00:06:56,385 --> 00:07:00,174
to close the file, very simply file dot what dot close

133
00:07:00,959 --> 00:07:01,739
and that's it.

134
00:07:02,179 --> 00:07:03,100
So now

135
00:07:03,359 --> 00:07:04,970
if I run the program again,

136
00:07:05,109 --> 00:07:06,970
you can say example, the text is over there,

137
00:07:06,980 --> 00:07:08,619
I can open up the file and it's right there.

138
00:07:08,630 --> 00:07:09,869
Hello world.

139
00:07:10,500 --> 00:07:17,820
The reason why we prefer or most developers prefer to use the with statement

140
00:07:18,170 --> 00:07:21,570
is because with the with statement, let me just undo all of this.

141
00:07:21,750 --> 00:07:23,500
With the with statement,

142
00:07:23,640 --> 00:07:27,359
you would notice that we didn't actually

143
00:07:27,739 --> 00:07:31,200
I have to close the file manually.

144
00:07:31,630 --> 00:07:35,920
Python is smart enough to know that. OK, when you're using the width statements

145
00:07:36,250 --> 00:07:39,029
as soon as you've performed whatever operation it is

146
00:07:39,040 --> 00:07:40,779
that you want to perform on the file,

147
00:07:40,799 --> 00:07:43,239
close it automatically.

148
00:07:43,690 --> 00:07:47,290
That's why developers often use the width statement.

149
00:07:47,500 --> 00:07:49,109
You don't have to close the file anymore

150
00:07:49,119 --> 00:07:51,670
because Python will do so automatically plus,

151
00:07:51,679 --> 00:07:52,730
it's also cleaner.

152
00:07:52,739 --> 00:07:54,640
You have less lines of code.

153
00:07:54,839 --> 00:07:58,480
That's why the width statement is typically used in conjunction

154
00:07:58,589 --> 00:08:00,869
with the open function.

155
00:08:01,149 --> 00:08:04,549
So thank you for watching. I will see you in the next class.

