1
00:00:05,320 --> 00:00:07,400
So we're now gonna start
talking about reading and

2
00:00:07,400 --> 00:00:09,330
writing binary files in Python.

3
00:00:09,330 --> 00:00:12,060
Now, there's two main reasons why
we would wanna deal with that,

4
00:00:12,060 --> 00:00:15,170
why we would wanna deal with binary files.

5
00:00:15,170 --> 00:00:19,300
So either we're processing binary data,
such as for an example, an image file,

6
00:00:19,300 --> 00:00:22,930
or we might want to store
the variables in our program, so

7
00:00:22,930 --> 00:00:24,620
that we can load them back in later.

8
00:00:24,620 --> 00:00:26,920
Now the good thing is that
the basic principles of reading and

9
00:00:26,920 --> 00:00:31,800
writing text files can be applied
here, in principle, in both cases.

10
00:00:31,800 --> 00:00:34,170
So what we'll do is we'll start
writing some binary data,

11
00:00:34,170 --> 00:00:36,230
and then we'll read it back in again.

12
00:00:36,230 --> 00:00:40,340
Now creating a binary file is just
as easy as writing a text file

13
00:00:40,340 --> 00:00:42,980
What we do is we just specify
the mode as b for binary.

14
00:00:42,980 --> 00:00:44,650
Thing to keep in mind is that strings and

15
00:00:44,650 --> 00:00:47,720
integers cannot be directly
written to a binary file.

16
00:00:47,720 --> 00:00:51,080
They need to be converted to
a format called Bytes first.

17
00:00:51,080 --> 00:00:54,800
Now Python provides a number of ways to do
this, and the most obvious being the method

18
00:00:54,800 --> 00:00:58,560
I'm about to show you; which is
using the bytes built-in function.

19
00:00:58,560 --> 00:01:03,270
And then the to_bytes method of
integer objects, which does just that.

20
00:01:03,270 --> 00:01:06,500
So, lets start out now by writing the
numbers from zero to sixteen to a binary

21
00:01:06,500 --> 00:01:08,330
file, and then reading them back in again.

22
00:01:08,330 --> 00:01:10,910
So, you're going to start typing with,

23
00:01:10,910 --> 00:01:13,580
we're going to be using with
like we did with text files.

24
00:01:13,580 --> 00:01:16,360
Open and we'll call the file binary.

25
00:01:16,360 --> 00:01:19,990
And we're going to put b for
binary and w for write.

26
00:01:21,570 --> 00:01:24,980
Notice that if we don't put the b there,
it's going to assume to be a text file.

27
00:01:24,980 --> 00:01:29,410
So we need to put the b as well as
the w for write mode, for binary files.

28
00:01:29,410 --> 00:01:31,260
We'll call that bin_file.

29
00:01:31,260 --> 00:01:33,440
Then we'll put

30
00:01:34,610 --> 00:01:39,660
for i in range 17, because you want
the numbers from 0 to 16 to be processed.

31
00:01:44,000 --> 00:01:48,570
then we wanna put bin_file.write bytes.

32
00:01:48,570 --> 00:01:52,920
And bytes is the in built
function that comes with Python.

33
00:01:52,920 --> 00:01:57,900
Then we'll just put i. So what we're
doing is we're converting the number i

34
00:01:57,900 --> 00:02:00,510
to a byte format, and
then writing that to our binary file.

35
00:02:02,170 --> 00:02:03,990
And that should be like so.

36
00:02:06,000 --> 00:02:11,070
And then to read it, we come back here and

37
00:02:11,070 --> 00:02:18,730
we type with open binary,
and specify the mode as b again

38
00:02:18,730 --> 00:02:24,520
for binary, and r for read,
as bin_file.

39
00:02:24,520 --> 00:02:27,240
Actually we should make that
different to the first one.

40
00:02:30,270 --> 00:02:33,660
Binfile we'll call that one this time.

41
00:02:33,660 --> 00:02:35,940
Binfile print b.

42
00:02:35,940 --> 00:02:39,420
So there's our basic code, so

43
00:02:39,420 --> 00:02:44,890
if we now run that, and you can see
the output on the screen there.

44
00:02:44,890 --> 00:02:47,050
So opening a binary file
in order to write it,

45
00:02:47,050 --> 00:02:51,250
it's very similar in the examples we used
for writing to a text file, as I mentioned.

46
00:02:51,250 --> 00:02:54,150
We just specify b, which you
can't see on the screen here now.

47
00:02:55,460 --> 00:02:56,640
Oh, actually you could see that.

48
00:02:56,640 --> 00:02:59,430
So that is b in front of the mode.

49
00:02:59,430 --> 00:03:03,620
So it's gonna be bw for
writing binary, br for reading.

50
00:03:03,620 --> 00:03:05,330
And you didn't see this
before previously, but

51
00:03:05,330 --> 00:03:09,230
when we're writing, we now
use the bin_file identifier.

52
00:03:09,230 --> 00:03:14,260
And we use a .write method, that's part of
that, to write our bytes out in that order.

53
00:03:14,260 --> 00:03:17,210
So that's a little bit different
to what you've seen before.

54
00:03:17,210 --> 00:03:21,260
And of course, the other clear thing there
is the bytes method there on line three.

55
00:03:21,260 --> 00:03:26,520
We have to use that built in bytes method, to
convert our data, in this case an integer, to

56
00:03:26,520 --> 00:03:31,770
a bytes object which can be written
to the binary file, effectively.

57
00:03:31,770 --> 00:03:35,580
So that method on line three,
the bytes method, returns a bytes object.

58
00:03:35,580 --> 00:03:38,780
And that's an immutable version of a byte
array, which we'll be looking at later.

59
00:03:38,780 --> 00:03:42,560
And essentially it's just a sequence
of integers in the range 0 to 255,

60
00:03:42,560 --> 00:03:44,430
in other words, a sequence of bytes.

61
00:03:44,430 --> 00:03:48,190
And you can see that in the example, that
I've actually shown when we actually ran

62
00:03:48,190 --> 00:03:50,930
the output, when we ran the program.

63
00:03:50,930 --> 00:03:55,050
Now the bytes function itself doesn't
quite behave as we might have expected.

64
00:03:55,050 --> 00:03:57,520
And we'll discuss line
three in a bit more detail

65
00:03:57,520 --> 00:04:00,780
after we've talked a bit more about the file
that was created, here to the right.

66
00:04:00,780 --> 00:04:04,690
So your binary file should be saved in
the same directory as your Python program.

67
00:04:04,690 --> 00:04:09,400
In my case, now, it's going to be there
in my PythonProgram/binary.

68
00:04:09,400 --> 00:04:12,990
So what I should be able to do is, I
should be able right click my file,

69
00:04:12,990 --> 00:04:15,890
click on Reveal in Finder,
to go to that folder directly.

70
00:04:15,890 --> 00:04:18,940
And you can see there is
the binary file at the top here.

71
00:04:18,940 --> 00:04:23,370
And we can also come back here, and
the binary file should be there now.

72
00:04:23,370 --> 00:04:26,050
We can double click that.
IntelliJ will come up and say,

73
00:04:26,050 --> 00:04:29,970
do you wanna register a new file type, cuz
it doesn't recognize this type of file.

74
00:04:29,970 --> 00:04:32,450
Clearly we're getting different results,
I'm not gonna actually open that now.

75
00:04:32,450 --> 00:04:36,900
But clearly the point is that it's not
a text file anymore, and it can't be read

76
00:04:36,900 --> 00:04:41,200
as we would read a normal text file, because we're
using a different process to write these.

77
00:04:41,200 --> 00:04:46,260
The very nature of binary files is often
they're using non-text characters.

78
00:04:46,260 --> 00:04:49,250
So again, we can go back and have a look,
and try to open this file.

79
00:04:51,220 --> 00:04:54,780
And you see the Mac hasn't done a good job of that,
it's not displaying anything there at all.

80
00:04:54,780 --> 00:04:56,830
As far as its concerned it doesn't exist,
so that's no good.

81
00:04:56,830 --> 00:04:59,640
So let's go back to IntelliJ and
we'll double click it again.

82
00:04:59,640 --> 00:05:04,360
And this time we'll just get it to use
the text file, just to see what happens,

83
00:05:04,360 --> 00:05:05,660
see what will come back.

84
00:05:05,660 --> 00:05:08,130
In this case you can see,
again, it wasn't very helpful.

85
00:05:08,130 --> 00:05:10,880
Basically displays a non-text
character as a square.

86
00:05:10,880 --> 00:05:13,250
And we can't really do much,
we can't go over.

87
00:05:13,250 --> 00:05:17,730
But essentially that's because the format
of the file isn't a format that you can

88
00:05:17,730 --> 00:05:20,930
normally read from, in the tradition
sense, that as a text file is.

89
00:05:20,930 --> 00:05:24,050
And just to confirm this is the actual
output on the right hand side here.

90
00:05:24,050 --> 00:05:27,180
This is the data that got written, so
you can see it's in a weird format,

91
00:05:27,180 --> 00:05:28,480
the slash means it's a

92
00:05:28,480 --> 00:05:33,370
Character code. So if you look carefully
you can see that we're using \x for

93
00:05:33,370 --> 00:05:35,610
number zero, number one, number two,
number three, 4-5-6-7-8.

94
00:05:35,610 --> 00:05:39,610
And then we're getting into
hex as you can see there.

95
00:05:39,610 --> 00:05:43,980
We count through eventually to ten,
which is hex for the number 16.

96
00:05:43,980 --> 00:05:47,770
Other thing to point in mind is
the \t there for tab. And

97
00:05:47,770 --> 00:05:52,590
that's because the number code for
a byte 09 or

98
00:05:52,590 --> 00:05:56,140
character 09 is actually known as a tab.

99
00:05:56,140 --> 00:05:59,900
And for 10, that's also known
as a return,

100
00:05:59,900 --> 00:06:02,090
or a new line character,
to be more precise,

101
00:06:02,090 --> 00:06:05,300
in case you are wondering what
they are. And d is also missing.

102
00:06:05,300 --> 00:06:08,230
It's in fact replaced by \r. And

103
00:06:08,230 --> 00:06:11,930
that's because that's a carriage a return
character, which has got the value 13.

104
00:06:11,930 --> 00:06:16,300
So, what's happening is there's
been some sort of conversion here.

105
00:06:16,300 --> 00:06:19,090
IntelliJ knows that some of these
are common codes that are used ,and

106
00:06:19,090 --> 00:06:22,780
its actually showing what
the representation of those are, when

107
00:06:22,780 --> 00:06:24,430
we're outputting this list.

108
00:06:24,430 --> 00:06:28,460
Now, if we actually did this, if we
actually output this on a Linux console

109
00:06:28,460 --> 00:06:32,860
x07 is a bell character, so,
not sure whether that will work on a Mac.

110
00:06:32,860 --> 00:06:36,050
But we can go to the folder,
I'm just going to my terminal.

111
00:06:37,330 --> 00:06:43,560
And if I go cd
~/Documents/Programs/PythonPrograms/Binary

112
00:06:43,560 --> 00:06:47,290
I'm going to go into that folder,
and if I try and

113
00:06:47,290 --> 00:06:51,800
do a cat, which is the equivalent of
Of typing the file contents out.

114
00:06:51,800 --> 00:06:53,780
[SOUND] Did it again.

115
00:06:53,780 --> 00:06:56,000
[SOUND] Did you hear that beep?

116
00:06:56,000 --> 00:07:01,110
That's the beep for the character 7,
x07, which is a beep character.

117
00:07:01,110 --> 00:07:03,690
So you'll find that you'll get a beep
if you run this under Linux as well, or

118
00:07:03,690 --> 00:07:05,210
if you try and type this.

119
00:07:05,210 --> 00:07:07,160
And I think you may get
that on Windows as well.

120
00:07:07,160 --> 00:07:11,710
So if you go to that folder and
type in the commands type and

121
00:07:11,710 --> 00:07:15,370
then space and then name - in this case
binary, you'll probably get the same thing.

122
00:07:15,370 --> 00:07:16,970
You'll get some sort of beep for

123
00:07:16,970 --> 00:07:22,700
the character, because it's the character
that would actually beep in Windows as well.

124
00:07:22,700 --> 00:07:24,400
So getting back to line three in our code.

125
00:07:24,400 --> 00:07:26,100
I'll close down the binary folder now.

126
00:07:26,100 --> 00:07:27,200
And closing the run window now.

127
00:07:27,200 --> 00:07:29,330
Now getting back to line three,

128
00:07:29,330 --> 00:07:33,300
did you notice that we actually passed
the list to the bytes function?

129
00:07:33,300 --> 00:07:37,500
And the reason that we passed the list,
that we didn't just pass i without a list

130
00:07:37,500 --> 00:07:42,100
or not in a list, is because
byte works a little strangely.

131
00:07:42,100 --> 00:07:43,480
So if you pass an integer to it,

132
00:07:43,480 --> 00:07:46,920
it creates a byte sequence with
that many bytes, all set to zero.

133
00:07:46,920 --> 00:07:47,900
Which is not what we wanted here.

134
00:07:47,900 --> 00:07:50,060
So by enclosing i in square brackets,

135
00:07:50,060 --> 00:07:52,650
we're parsing a list with
the single item i, which

136
00:07:52,650 --> 00:07:56,530
the bytes function then correctly converted
to a single byte, which you saw in the output.

137
00:07:56,530 --> 00:07:59,170
If we fail to convert the number itself
to a byte, we'll get an error.

138
00:07:59,170 --> 00:08:03,140
We can see that by changing line three,

139
00:08:03,140 --> 00:08:07,230
and temporarily removing that code to
convert it to a byte and also the list.

140
00:08:07,230 --> 00:08:10,490
And we try and create it
like that and run it.

141
00:08:10,490 --> 00:08:13,570
We get an error,
bytes-like object is required, not 'int'.

142
00:08:13,570 --> 00:08:16,260
So we do need to do that
conversion, before we try and

143
00:08:16,260 --> 00:08:19,990
save it as a byte in binary
format to a binary file.

144
00:08:19,990 --> 00:08:24,590
Now we'll have a quick look now on how we
can write numerical values greater than

145
00:08:24,590 --> 00:08:25,960
255 to a binary file.

146
00:08:25,960 --> 00:08:30,540
But first one question you might be asking
now is, if we have to pass a sequence type,

147
00:08:30,540 --> 00:08:35,179
such as list, to bytes, to the bytes method,
and I'll just undo that so we can see it again.

148
00:08:35,179 --> 00:08:38,039
So if you need to pass
a list as you saw there, and

149
00:08:38,039 --> 00:08:40,390
we generated the sequence
from the range function,

150
00:08:40,390 --> 00:08:41,870
on the previous line,

151
00:08:41,870 --> 00:08:45,110
Why do we bother iterating through it
to create a single item for each value.

152
00:08:45,110 --> 00:08:48,910
Now the answer is that we
shouldn't have done that, but

153
00:08:48,910 --> 00:08:52,580
really what we wanted to do was just
to demonstrate what was happening.

154
00:08:52,580 --> 00:08:56,450
In practice you wouldn't create some
code exactly like we've done here.

155
00:08:56,450 --> 00:08:59,810
You'd actually write the program in one
less line, by just adding the range to

156
00:08:59,810 --> 00:09:00,790
the bytes function.

157
00:09:00,790 --> 00:09:03,090
So in other words you
do something like this.

158
00:09:03,090 --> 00:09:07,500
You get rid of the for
loop altogether, and

159
00:09:07,500 --> 00:09:12,890
instead of putting bytes and
putting a list there,

160
00:09:12,890 --> 00:09:18,030
we put (bytes(range(17))), Like so.

161
00:09:18,030 --> 00:09:23,200
And if we run that, you can see
we've got exactly the same result,

162
00:09:23,200 --> 00:09:25,770
in terms of output, with one less line of code.

163
00:09:25,770 --> 00:09:30,300
Now it'd be more useful, with binary files,
to store the values of our own numerical

164
00:09:30,300 --> 00:09:32,340
values in a binary file, and we can do that.

165
00:09:32,340 --> 00:09:37,650
And it turns out the integer object itself's
got a to_bytes method that allows us to do that.

166
00:09:37,650 --> 00:09:40,360
So this next example,
what I'm going to do is

167
00:09:40,360 --> 00:09:44,620
write the value out of four variables, and
allow to read those values back in again.

168
00:09:44,620 --> 00:09:46,800
Going to close this and
I'm just going to that code out.

169
00:09:46,800 --> 00:09:52,380
And I'm going to type, well we're
going to set the values first.

170
00:09:52,380 --> 00:10:01,240
A equals 65534, and that's equivalent
to FF FE, and A equals 65535

171
00:10:01,240 --> 00:10:06,150
That's equivalent to FF FF.

172
00:10:06,150 --> 00:10:07,750
C equals 65536.

173
00:10:07,750 --> 00:10:15,450
That's equivalent to 00010000 And

174
00:10:15,450 --> 00:10:21,190
finally D equals 2998302,
that's equivalent to O2, sorry

175
00:10:21,190 --> 00:10:26,580
002DCO1E, so that's our four variables,

176
00:10:26,580 --> 00:10:30,210
and I'm just giving you the hex
equivalents there, to the right hand side,

177
00:10:30,210 --> 00:10:37,130
And lets write some code. So we can put right,
so I can put with, open binary2.

178
00:10:37,130 --> 00:10:42,060
And b w because we want to
write a binary file format.

179
00:10:42,060 --> 00:10:42,720
as bin_file.

180
00:10:44,830 --> 00:10:47,100
I'm going to put bin_file.write

181
00:10:48,380 --> 00:10:53,320
a.to_bytes, we can see the to_bytes method
and we're converting the integer

182
00:10:53,320 --> 00:10:56,350
Automatically to bytes. And then we're
gonna pass an extra parameter here,

183
00:10:56,350 --> 00:10:59,660
we're gonna put the length, and
we're gonna put the byte order. And

184
00:10:59,660 --> 00:11:00,740
we'll talk about that shortly.

185
00:11:00,740 --> 00:11:01,990
I'm gonna set that to big.

186
00:11:04,080 --> 00:11:06,730
And this d, we'll set that to d to just be
a little bit different, because there's

187
00:11:06,730 --> 00:11:07,570
a reason for that shortly.

188
00:11:07,570 --> 00:11:09,350
We'll just do the same for the other two.

189
00:11:10,600 --> 00:11:11,760
In fact all three variables.

190
00:11:11,760 --> 00:11:15,950
The next one is going to be to_bytes
again, so that's going to be b.

191
00:11:15,950 --> 00:11:19,920
We're basing that on the fact that
this representation over here,

192
00:11:19,920 --> 00:11:22,830
so for a and b we only needed
two bytes to store the value.

193
00:11:22,830 --> 00:11:25,500
But the next one, because we've
clocked over the 65563 mark,

194
00:11:25,500 --> 00:11:27,360
we need to use four bytes.

195
00:11:27,360 --> 00:11:28,880
So I'm going to set that to 4.

196
00:11:32,720 --> 00:11:38,300
And the last one, which will be x, that
also needs four bytes as you can see,

197
00:11:38,300 --> 00:11:39,780
so I'm going to change that to 4.

198
00:11:39,780 --> 00:11:43,810
And then I'm going to do something extra,
and then we'll discuss it.

199
00:11:43,810 --> 00:11:48,710
So I'm going to set that to write another
variable out, x again, this time I'm going

200
00:11:48,710 --> 00:11:53,910
to leave it on 4, but I'm going
to change this to little, like so.

201
00:11:53,910 --> 00:11:54,840
So first of all, run it.

202
00:11:56,840 --> 00:11:59,940
And whoops, I made a typo there,
so that should've been a, b, c.

203
00:11:59,940 --> 00:12:02,630
And I'll run it again.

204
00:12:02,630 --> 00:12:05,850
[LAUGH] I'm having a hard
time typing at the moment.

205
00:12:05,850 --> 00:12:08,270
little, that's meant to be,
so we'll try that again.

206
00:12:08,270 --> 00:12:10,350
Third time lucky, run it.

207
00:12:10,350 --> 00:12:14,130
And we've got no output, which means that,
well no errors, which is a good start,

208
00:12:14,130 --> 00:12:16,430
and we haven't got any
output in our program.

209
00:12:16,430 --> 00:12:19,750
So it's unsurprising that we haven't
got anything showing on the screen.

210
00:12:19,750 --> 00:12:22,280
So for these two parameters to
the to_bytes method,

211
00:12:22,280 --> 00:12:24,740
the first one is the number
of bytes we want, and

212
00:12:24,740 --> 00:12:28,150
the second is whether to return the result
as big endian or little endian.

213
00:12:28,150 --> 00:12:30,600
Now the number of bytes
is straight forward.

214
00:12:30,600 --> 00:12:33,830
Cuz if you remember from the earlier
lecture on binary, the largest number

215
00:12:33,830 --> 00:12:37,410
we can store in two bytes, or
16 bits in other words, was 65535.

216
00:12:37,410 --> 00:12:41,990
As soon as we get to a higher number, we need
to go to more bytes to store those numbers.

217
00:12:41,990 --> 00:12:47,490
And in this case, for c, we need at least 3
bytes to represent that variable.

218
00:12:47,490 --> 00:12:51,170
But it's usual when you're writing binary
to use an even number of bytes, and so

219
00:12:51,170 --> 00:12:54,320
that's why we've converted
it up to 4 in that case.

220
00:12:54,320 --> 00:12:59,200
Now endianness arose, this is that little and big here, the second parameter so

221
00:12:59,200 --> 00:13:03,100
this endiannes arose when computer
manufacturers had to make a decision

222
00:13:03,100 --> 00:13:05,540
about how to store numbers
in their computer's memory.

223
00:13:05,540 --> 00:13:10,120
So what happens is big endian stores
the most significant byte first

224
00:13:10,120 --> 00:13:12,770
with the remaining bytes of
the number following in order,

225
00:13:12,770 --> 00:13:15,220
the same way as you'd write
it in other words.

226
00:13:15,220 --> 00:13:18,070
But little endian is the reverse
of this. In other words, it shows

227
00:13:18,070 --> 00:13:20,950
the least significant
byte being stored first.

228
00:13:20,950 --> 00:13:23,560
Now the initial choice
was largely arbitrary.

229
00:13:23,560 --> 00:13:27,190
With, for example, IBM choosing
big endian for their mainframes.

230
00:13:27,190 --> 00:13:28,220
This is years ago.

231
00:13:28,220 --> 00:13:32,720
And Intel, Intel CPUs and Intel the
manufacturer, are using little endian for

232
00:13:32,720 --> 00:13:34,480
their early microprocessors.

233
00:13:34,480 --> 00:13:37,040
So once the choice has been made,
it continues to be used and

234
00:13:37,040 --> 00:13:38,970
maintain backwards compatibility.

235
00:13:38,970 --> 00:13:42,700
Now we've chosen, in this example,
big endian for all but the last write.

236
00:13:42,700 --> 00:13:47,000
And as you can see there by passing
big for the first four calls

237
00:13:47,000 --> 00:13:50,580
we used big endian, and the only
difference is the fifth one, when

238
00:13:50,580 --> 00:13:54,540
we've written a value of x again, and we've actually
used little endian format for that time.

239
00:13:54,540 --> 00:13:57,180
What we're gonna do is have a quick look
at reading the numbers back into our

240
00:13:57,180 --> 00:14:01,470
program, before we move on to more Pythonic
ways of storing a number of variables

241
00:14:01,470 --> 00:14:02,520
to a binary file.

242
00:14:02,520 --> 00:14:03,210
So that's the write.

243
00:14:03,210 --> 00:14:06,330
Let's write some code now that's
going to read those values back.

244
00:14:06,330 --> 00:14:11,030
So I'm gonna put with open binary2.

245
00:14:11,030 --> 00:14:16,370
We're gonna do a br mode,
to read it this time.

246
00:14:16,370 --> 00:14:17,890
And we're gonna call it binFile.

247
00:14:17,890 --> 00:14:18,730
Let's use a different name.

248
00:14:18,730 --> 00:14:19,820
We don't really have to, but I will.

249
00:14:19,820 --> 00:14:25,110
And what we'll do is, we'll assign
some new variables, e, f, g, h, i.

250
00:14:25,110 --> 00:14:28,280
Five variables for the five values
that are now in that file.

251
00:14:28,280 --> 00:14:33,710
So we can put e = int.from_bytes

252
00:14:33,710 --> 00:14:36,890
and it's the opposite to to_bytes.
from is converting it from the file.

253
00:14:36,890 --> 00:14:40,770
We are going to put binfile,
or binFile.

254
00:14:40,770 --> 00:14:42,810
Just to show you that we
can't do it that way,

255
00:14:42,810 --> 00:14:47,050
we are going to use the same
identifier. So we'll call that bin_file.

256
00:14:47,050 --> 00:14:50,150
Because there isn't a reason now
that we're finished with the first

257
00:14:51,500 --> 00:14:52,810
with, on line 13,
that we can't reuse that.

258
00:14:52,810 --> 00:14:56,020
So equals int.from_bytes bin file and

259
00:14:56,020 --> 00:14:59,970
then you need to pass the number of bytes,
in this case two.

260
00:14:59,970 --> 00:15:03,450
Two, and then the format whether it's big
or little, we're going to put big, like so.

261
00:15:03,450 --> 00:15:09,000
So that's creating a variable called e,
and we'll do the same for f, g, h, i.

262
00:15:09,000 --> 00:15:13,980
So it's f, g, h, i.

263
00:15:13,980 --> 00:15:19,990
And of course, that represents,
or relates to, e is variable a,

264
00:15:19,990 --> 00:15:25,910
f is b, g is c, h is x but
with big-endian format,

265
00:15:25,910 --> 00:15:30,500
and i is that same variable x
again in little-endian format.

266
00:15:30,500 --> 00:15:33,350
So for the last one,
we've purposely left it as big again.

267
00:15:33,350 --> 00:15:36,310
So we're reading them all
back in big format again.

268
00:15:36,310 --> 00:15:39,070
The other thing I need to do is
change the last three there to make

269
00:15:39,070 --> 00:15:43,410
sure we're storing the correct number of,
or reading the correct number of bytes.

270
00:15:43,410 --> 00:15:48,050
And relates completely to the number that
we used when we created the binary file.

271
00:15:48,050 --> 00:15:51,100
So if we run that again now, actually
what I'll obviously need to do first,

272
00:15:51,100 --> 00:15:52,980
is put a print in there.

273
00:15:52,980 --> 00:15:57,070
Print e, because we want to see the result
and confirm they're actually the same,

274
00:15:57,070 --> 00:16:01,230
and we'll just change all those now.

275
00:16:01,230 --> 00:16:05,850
So f g h i.

276
00:16:05,850 --> 00:16:10,880
So now if we run that. And
I missed a method didn't I?

277
00:16:10,880 --> 00:16:15,400
I should have put .read because
I specified the name of

278
00:16:15,400 --> 00:16:19,290
the file identifier, but
I didn't put .read so

279
00:16:19,290 --> 00:16:24,920
we need to actually call the method
.read, to do the opposite of .write and

280
00:16:24,920 --> 00:16:28,300
to read the data from, of course,
the binary file that we've created.

281
00:16:31,620 --> 00:16:33,240
Okay, so we have a .read for all of those.

282
00:16:34,750 --> 00:16:37,240
Now, try again, run it again.

283
00:16:37,240 --> 00:16:38,620
And this time we've got some results.

284
00:16:38,620 --> 00:16:42,690
And you can see that the first
four examples that have come back.

285
00:16:42,690 --> 00:16:44,090
This is e, f, g and h.

286
00:16:44,090 --> 00:16:46,190
You can see they should represent,

287
00:16:46,190 --> 00:16:49,310
well they do represent, the exact numbers
that we originally saved them in.

288
00:16:49,310 --> 00:16:53,390
But the fifth one is what we
want you to have a look at.

289
00:16:53,390 --> 00:16:58,960
Notice how that number is now
completely different to the x value,

290
00:16:58,960 --> 00:17:00,930
2998302, that's on line 11.

291
00:17:00,930 --> 00:17:03,660
And what's happened there is,
I'll just close this down so

292
00:17:03,660 --> 00:17:05,030
you can see it a bit easier.

293
00:17:05,030 --> 00:17:09,240
We used big format again here. So
we saved it in little endian format and

294
00:17:09,240 --> 00:17:12,050
we tried to read it back
again in big endian format.

295
00:17:12,050 --> 00:17:15,760
So in other words the bytes are in the
reverse order, because they were written

296
00:17:15,760 --> 00:17:18,690
Initially in little endian
format on line 18 and

297
00:17:18,690 --> 00:17:22,280
we're trying to read them back
in big endian format in line 29.

298
00:17:22,280 --> 00:17:26,410
This demonstrates that it's very
important to understand the structure

299
00:17:26,410 --> 00:17:30,710
of the binary data in a file, if you're
going to attempt to read it successfully.

300
00:17:30,710 --> 00:17:34,400
Now, again I just want to make a point here
that this is pretty low level stuff

301
00:17:34,400 --> 00:17:37,410
So you can come out of this knowing that it
is possible, but it's a bit fiddly if all

302
00:17:37,410 --> 00:17:40,830
you want to do is store your program's
data, and read it back in again.

303
00:17:40,830 --> 00:17:41,410
But luckily,

304
00:17:41,410 --> 00:17:46,460
with that said, Python provides a couple of
other methods with dealing with this data.

305
00:17:46,460 --> 00:17:49,830
And to make things easier for
you to do something along these lines.

306
00:17:49,830 --> 00:17:51,810
So we'll move on now and
look at pickle and

307
00:17:51,810 --> 00:17:55,560
shelf, which are two ways of working
with binary data, in the next video

