1
00:00:05,825 --> 00:00:07,680
Hi, welcome to this new video.

2
00:00:07,680 --> 00:00:08,758
Welcome to week five.

3
00:00:08,758 --> 00:00:11,333
This is our final week of this course.

4
00:00:11,333 --> 00:00:16,077
So in week four we ended up talking
a little bit about how do you

5
00:00:16,077 --> 00:00:19,064
write an image into our disk, right?

6
00:00:19,064 --> 00:00:20,228
How do we export an image?

7
00:00:20,228 --> 00:00:22,335
Or how do we import images as well?

8
00:00:22,335 --> 00:00:26,629
So we're kind of starting to understand
a little bit the notion of inputs and

9
00:00:26,629 --> 00:00:27,240
outputs.

10
00:00:27,240 --> 00:00:31,227
We're going to extend that idea with
the idea of reading and writing text.

11
00:00:31,227 --> 00:00:35,794
Because many times you might have like
a data set or some kind of information and

12
00:00:35,794 --> 00:00:40,153
you want to maybe visualize that, or
perhaps you want to collect your data or

13
00:00:40,153 --> 00:00:43,561
even save the state of a program
as a save file, all right?

14
00:00:43,561 --> 00:00:45,328
So we're going to start
learning how to do that.

15
00:00:45,328 --> 00:00:48,068
So let's just kind of understand
the notion of inputs and outputs, right.

16
00:00:48,068 --> 00:00:50,794
When we read a file,
we consider that the input,

17
00:00:50,794 --> 00:00:53,930
we bring that into the internal
memory of our software.

18
00:00:53,930 --> 00:00:56,873
When we write a file,
that's our output, right.

19
00:00:56,873 --> 00:00:59,723
And we can actually create
certain circularity.

20
00:00:59,723 --> 00:01:03,909
We could actually start thinking of maybe
a save file that we write initially when

21
00:01:03,909 --> 00:01:06,738
we want to store,
let's say the state of a program, and

22
00:01:06,738 --> 00:01:10,324
then eventually we want to run again and
maybe load that file, right?

23
00:01:10,324 --> 00:01:14,724
You could easily create a save system for
your compositions and

24
00:01:14,724 --> 00:01:19,386
maybe resume where you left off by
using some of these techniques.

25
00:01:19,386 --> 00:01:22,646
But we're going to start learning
how to do a very simple file.

26
00:01:22,646 --> 00:01:25,365
How do we write txt file?

27
00:01:25,365 --> 00:01:29,400
There's going to be several file
formats that we're going to cover and

28
00:01:29,400 --> 00:01:33,219
see how we can actually get into
more complex data structures,

29
00:01:33,219 --> 00:01:35,539
especially towards the next course.

30
00:01:35,539 --> 00:01:39,470
But for now we're going to
write a simple txt file.

31
00:01:39,470 --> 00:01:43,615
And for that we're going to use
the inbuilt function create writer,

32
00:01:43,615 --> 00:01:47,328
that it's a kind of a processing
function to define a file.

33
00:01:47,328 --> 00:01:51,598
In this case a txt file, and
how we could actually print line and

34
00:01:51,598 --> 00:01:56,515
then use a couple of functions like
flush and close to commit to memory and

35
00:01:56,515 --> 00:01:58,464
close the program, right?

36
00:01:58,464 --> 00:02:02,979
So in the case of the reading of a file,
we're going to use a different class,

37
00:02:02,979 --> 00:02:05,717
not the create writer, but reader, right.

38
00:02:05,717 --> 00:02:09,964
In this we will actually use the input
of an external file, such as data txt.

39
00:02:09,964 --> 00:02:14,875
This file would need to exist in
the folder similar to images, right.

40
00:02:14,875 --> 00:02:18,398
We will be using the same
folder to access that data,

41
00:02:18,398 --> 00:02:22,336
and then we can actually go
through each line of that file.

42
00:02:22,336 --> 00:02:26,785
And in this case we're going to be
using a while statement to just loop

43
00:02:26,785 --> 00:02:30,287
through the number of lines
that we have available.

44
00:02:30,287 --> 00:02:33,359
And eventually just going
through all of them.

45
00:02:33,359 --> 00:02:38,699
So let's jump into processing and
see how we can actually execute this.

46
00:02:38,699 --> 00:02:41,020
How do we implement some of these ideas?

47
00:02:41,020 --> 00:02:43,261
Okay, so as we save our file,

48
00:02:43,261 --> 00:02:48,025
we need to be able to locate
the folder in which we save our file.

49
00:02:48,025 --> 00:02:52,990
Because this is where anything that
we write out is going to be saved.

50
00:02:52,990 --> 00:02:56,504
And anything that we read in is going to
be obtained from the resources,

51
00:02:56,504 --> 00:02:58,998
if you want,
that we're going to have available.

52
00:02:58,998 --> 00:03:04,239
So let's quickly use an output.

53
00:03:04,239 --> 00:03:12,058
We're going to start with writing,

54
00:03:12,058 --> 00:03:19,884
and create a writer file data txt.

55
00:03:19,884 --> 00:03:22,695
So once you open this line,

56
00:03:22,695 --> 00:03:28,574
we could use in this output
some functionality, right?

57
00:03:28,574 --> 00:03:33,199
I invite you to check out the processing
reference to what kind of

58
00:03:33,199 --> 00:03:35,092
functionality you have.

59
00:03:35,092 --> 00:03:38,820
Sometimes you might not know what kind
of functionality is available to me.

60
00:03:38,820 --> 00:03:42,933
Always referencing to the reference
files is going to be important.

61
00:03:42,933 --> 00:03:46,555
So let's say print line,

62
00:03:46,555 --> 00:03:52,326
we will say hello, and that's our file.

63
00:03:52,326 --> 00:03:57,561
And maybe when we're done
we could say output flush.

64
00:03:57,561 --> 00:04:02,611
This is a line that is making
sure that we are committing that

65
00:04:02,611 --> 00:04:09,187
information to an external state,
in this case our hard drive output close.

66
00:04:09,187 --> 00:04:13,885
And we want to also make sure that we
add this line to close the writer so

67
00:04:13,885 --> 00:04:18,016
that you don't end up with a file
that is still kind of open and

68
00:04:18,016 --> 00:04:21,512
thinking that could be in a writing state,
right?

69
00:04:21,512 --> 00:04:25,579
So let's just execute this line.

70
00:04:25,579 --> 00:04:29,459
We haven't drawn anything to the screen,
but that's not a problem.

71
00:04:29,459 --> 00:04:35,904
We have our text file here, we can
open it and we have our hello, right.

72
00:04:35,904 --> 00:04:41,297
So you can add more lines

73
00:04:41,297 --> 00:04:45,342
such as this one,

74
00:04:45,342 --> 00:04:50,472
hello world, right?

75
00:04:50,472 --> 00:04:55,297
Again, in this case we're not really using
the screen, so we are writing each line.

76
00:04:55,297 --> 00:05:00,529
Notice that we are not writing in the same
line, but rather different lines.

77
00:05:00,529 --> 00:05:08,212
So what happens if I use print?

78
00:05:08,212 --> 00:05:13,919
Let's just try that.

79
00:05:13,919 --> 00:05:19,089
So the difference between the line
print and print LN, ln for line,

80
00:05:19,089 --> 00:05:24,361
is that we would actually specify
that we move to a next line, right?

81
00:05:24,361 --> 00:05:29,998
So if you want to add multiple strings or
multiple numbers in one line.

82
00:05:29,998 --> 00:05:34,850
Notice that I kind of created
a bit of a space here not to have

83
00:05:34,850 --> 00:05:39,120
the word hello world written altogether,
right.

84
00:05:39,120 --> 00:05:43,702
The other thing that we can do,
we can put all this in a for loop, right?

85
00:05:43,702 --> 00:05:52,390
So for I in range 200, right.

86
00:05:52,390 --> 00:05:59,552
We could say,
let's just delete the dot for a moment.

87
00:05:59,552 --> 00:06:04,854
We can print something like,

88
00:06:04,854 --> 00:06:09,944
let's use the STR to convert

89
00:06:09,944 --> 00:06:15,892
into a string, the number 50.

90
00:06:15,892 --> 00:06:20,819
But you could certainly
do a random number here

91
00:06:20,819 --> 00:06:25,502
if you want 200 random numbers, right.

92
00:06:25,502 --> 00:06:28,975
We execute that.

93
00:06:28,975 --> 00:06:35,096
And you can see we have that sequence

94
00:06:35,096 --> 00:06:41,022
of number 50 if we do print line.

95
00:06:41,022 --> 00:06:45,794
And again, we can do the sequence

96
00:06:45,794 --> 00:06:50,063
as different lines, right?

97
00:06:50,063 --> 00:06:58,250
So, very useful way of basically
writing information into a text file.

98
00:06:58,250 --> 00:07:00,568
Again, a text file is
a very simple format.

99
00:07:00,568 --> 00:07:03,220
I don't tend to use text files too much.

100
00:07:03,220 --> 00:07:06,568
You'll see that some other kind of more
advanced file formats are going to be

101
00:07:06,568 --> 00:07:07,702
a lot more interesting.

102
00:07:07,702 --> 00:07:12,334
Things like CSV or JSON files that
we're going to learn a bit later.

103
00:07:12,334 --> 00:07:17,814
But for now, let's import the random
module and create a set of,

104
00:07:17,814 --> 00:07:22,698
let's imagine that we're
creating a data set of random,

105
00:07:22,698 --> 00:07:27,899
I don't know, coordinates or
values, something, right?

106
00:07:27,899 --> 00:07:33,440
So here we could say let's print

107
00:07:33,440 --> 00:07:37,956
the line random rand range

108
00:07:37,956 --> 00:07:42,478
between zero and 100.

109
00:07:42,478 --> 00:07:44,615
Let's see if we have
any problem with that.

110
00:07:44,615 --> 00:07:51,183
So we got our file and
you should see that all our

111
00:07:51,183 --> 00:07:56,417
numbers are in fact random, right?

112
00:07:56,417 --> 00:07:58,510
So that's our data, this is our writing.

113
00:07:58,510 --> 00:08:02,021
Let's just quickly jump and
see how we can read that file now.

114
00:08:02,021 --> 00:08:06,262
So let's, for a moment here,

115
00:08:06,262 --> 00:08:11,002
I'm going to just leave this aside.

116
00:08:11,002 --> 00:08:14,839
So this is our function.

117
00:08:14,839 --> 00:08:17,727
We're going to do a multi comment.

118
00:08:17,727 --> 00:08:24,384
So this is a way of, kind of, freezing
if you want this section of the code.

119
00:08:24,384 --> 00:08:28,287
Very useful if you have a lot of lines
that you want to kind of suppress for

120
00:08:28,287 --> 00:08:29,291
some time being.

121
00:08:29,291 --> 00:08:30,384
Let's do a reader

122
00:08:38,651 --> 00:08:44,429
I'm going to read the file
that we just created, right?

123
00:08:44,429 --> 00:08:50,220
That's our reader function.

124
00:08:50,220 --> 00:08:56,680
Sorry, the variable line,
it's going to be from the reader.

125
00:08:56,680 --> 00:09:03,510
We're going to access the read line,
I think capital l.

126
00:09:03,510 --> 00:09:05,008
There we go.

127
00:09:05,008 --> 00:09:09,894
So this line is interesting
because this line will advance

128
00:09:09,894 --> 00:09:14,785
basically the entity we're
currently reading, right?

129
00:09:14,785 --> 00:09:21,057
So while reader might contain all
the lines that we have available,

130
00:09:21,057 --> 00:09:26,008
the read line,
it's going to give us our current line,

131
00:09:26,008 --> 00:09:31,968
which is going to be the first one,
and we can continue doing that.

132
00:09:31,968 --> 00:09:39,381
So let's just print to
the console the line, right?

133
00:09:39,381 --> 00:09:44,870
What are we obtaining here, right?

134
00:09:44,870 --> 00:09:50,755
So you see here we have our first
value printed down here, right?

135
00:09:50,755 --> 00:09:53,722
So that's our first value.

136
00:09:53,722 --> 00:10:00,279
Let's imagine that we just copy
paste this line once again, right?

137
00:10:00,279 --> 00:10:03,902
So nothing has changed.

138
00:10:03,902 --> 00:10:09,572
Line, still kind of, we say read line,
and we print the next value.

139
00:10:09,572 --> 00:10:11,371
You see that the values change, right?

140
00:10:11,371 --> 00:10:14,456
So what this shows us
is that the read line,

141
00:10:14,456 --> 00:10:18,058
basically this line, or
the read line function,

142
00:10:18,058 --> 00:10:24,000
it's moving the head of the reader towards
the next line in the sequence, right?

143
00:10:24,000 --> 00:10:27,696
So we basically if we want to read
all the lines in the document,

144
00:10:27,696 --> 00:10:31,477
we have to create some form of
iteration or looping structure.

145
00:10:31,477 --> 00:10:33,391
But we don't know how
many lines were created.

146
00:10:33,391 --> 00:10:35,585
We obviously know because
we created the file.

147
00:10:35,585 --> 00:10:39,824
There were in fact 200 entries,
but we might not know.

148
00:10:39,824 --> 00:10:44,701
So let's use a while loop that it's

149
00:10:44,701 --> 00:10:49,096
a particularly good loop here.

150
00:10:49,096 --> 00:10:53,838
So while the line is not equal to none,

151
00:10:53,838 --> 00:10:57,988
meaning that there is no result,

152
00:10:57,988 --> 00:11:04,676
we will print the line and
also move the line, right.

153
00:11:04,676 --> 00:11:10,263
The most important thing is,
in fact to move the line.

154
00:11:10,263 --> 00:11:12,474
If we don't advance the head,

155
00:11:12,474 --> 00:11:17,407
we're going to end up looping forever
into this state, because we're

156
00:11:17,407 --> 00:11:22,356
going to be reading constantly
the first line of the document, right.

157
00:11:22,356 --> 00:11:25,442
But with this line,
we know that we're moving to the next one.

158
00:11:25,442 --> 00:11:29,861
So eventually we'll conclude reading
all the entries in the file.

159
00:11:29,861 --> 00:11:33,039
Let's see how this works.

160
00:11:33,039 --> 00:11:35,837
As you can see here, we can scroll and

161
00:11:35,837 --> 00:11:40,997
you can double check with your text
file that is saved in the folder.

162
00:11:40,997 --> 00:11:43,792
We should be able to read all those files,
right?

163
00:11:43,792 --> 00:11:46,283
So this is kind of a very
simple introduction.

164
00:11:46,283 --> 00:11:51,416
How text files can be generated
initially with the reader function,

165
00:11:51,416 --> 00:11:56,460
sorry, with the writer, and
then how we can read those text files or

166
00:11:56,460 --> 00:12:01,700
other text files that you might find
online with the reader function.

167
00:12:01,700 --> 00:12:05,232
And we're going to continue this week
looking at more advanced data types and

168
00:12:05,232 --> 00:12:07,880
see how we can actually create
more interesting files.

169
00:12:07,880 --> 00:12:09,703
So I'll see you in the next video