WEBVTT 1 00:00:02.180 --> 00:00:04.260 So we're not gonna start talking about reading and 2 00:00:04.260 --> 00:00:06.190 writing binary files in Python. 3 00:00:06.190 --> 00:00:08.920 Now, there's two main reasons why we would wanna deal with that, 4 00:00:08.920 --> 00:00:12.030 why we would wanna deal with binary files. 5 00:00:12.030 --> 00:00:16.160 So either we're processing binary data Such as for an example, an image file. 6 00:00:16.160 --> 00:00:19.790 And all we might want to store the variables in our program so 7 00:00:19.790 --> 00:00:21.480 that we can load them back in later. 8 00:00:21.480 --> 00:00:23.780 Now the good thing is that the basic principles of reading and 9 00:00:23.780 --> 00:00:28.660 writing text files can be applied here in principle in both cases. 10 00:00:28.660 --> 00:00:31.030 So what we'll do is we'll start writing some binary data, 11 00:00:31.030 --> 00:00:33.090 and then we'll read it back in again. 12 00:00:33.090 --> 00:00:37.200 Now creating a binary file is just as easy as writing a text file 13 00:00:37.200 --> 00:00:39.840 What we do is we just specify the mode as b for binary. 14 00:00:39.840 --> 00:00:41.510 Thing to keep in mind is that strings and 15 00:00:41.510 --> 00:00:44.580 integers cannot be directly written to a binary file. 16 00:00:44.580 --> 00:00:47.940 They need to be converted to a format called Bytes first. 17 00:00:47.940 --> 00:00:51.660 And Python provides a number of ways to do this and the most obvious being the method 18 00:00:51.660 --> 00:00:55.420 I'm about to show you which is using the Bytes build-in function. 19 00:00:55.420 --> 00:01:00.130 And then the two underscore Bytes method integer objects which does just that. 20 00:01:00.130 --> 00:01:03.360 So, lets start out now by writing the numbers from zero to sixteen to a binary 21 00:01:03.360 --> 00:01:05.190 fall and then ringing them back in again. 22 00:01:05.190 --> 00:01:07.770 So, you're going to start talking with, 23 00:01:07.770 --> 00:01:10.440 we're going to be using with like we did with text bowls. 24 00:01:10.440 --> 00:01:13.220 Open and we'll call the fall binary. 25 00:01:13.220 --> 00:01:16.850 And we're going to put B for binary and W for wright. 26 00:01:18.430 --> 00:01:21.840 Notice that if we don't put the b there it's going to assume to be a text file. 27 00:01:21.840 --> 00:01:26.270 So we need to put the b as well as the w for write mode and binary files. 28 00:01:26.270 --> 00:01:28.120 We'll call that sb number score file. 29 00:01:28.120 --> 00:01:30.300 Then we'll put 30 00:01:31.470 --> 00:01:36.526 firing range 17 because you want the numbers from 0 to 16 to be processed. 31 00:01:36.526 --> 00:01:40.860 Be put [INAUDIBLE] and 32 00:01:40.860 --> 00:01:45.430 then we wanna put bin_file.right bytes. 33 00:01:45.430 --> 00:01:49.780 And bytes is the in built function that comes with Python. 34 00:01:49.780 --> 00:01:54.760 Then we'll just put I, so what we're doing is we're converting the number I 35 00:01:54.760 --> 00:01:57.370 to a byte format, and then writing that to our binary file. 36 00:01:59.030 --> 00:02:00.850 And that should be like so. 37 00:02:02.860 --> 00:02:07.933 And then to read it, we come back here and 38 00:02:07.933 --> 00:02:15.593 we type with open("binary", 39 00:02:15.593 --> 00:02:21.380 'br') as bin_file. 40 00:02:21.380 --> 00:02:24.100 Actually we should make that dip into the first one. 41 00:02:27.130 --> 00:02:30.520 Binfile we'll call it on this time. 42 00:02:30.520 --> 00:02:32.800 Binfile print b. 43 00:02:32.800 --> 00:02:36.280 So there's our basic code, so 44 00:02:36.280 --> 00:02:41.750 if we now run that And you can see the output on the screen there. 45 00:02:41.750 --> 00:02:43.910 So opening a binary file in order to write it, 46 00:02:43.910 --> 00:02:48.110 it's very similar in the examples we used for writing to a text file as I mentioned. 47 00:02:48.110 --> 00:02:51.010 We just specify b, which you can't see on the screen here now. 48 00:02:52.320 --> 00:02:53.500 Oh, actually you could see that. 49 00:02:53.500 --> 00:02:56.290 So that is b in front of the mode. 50 00:02:56.290 --> 00:03:00.480 So it's gonna be bw for writing binary, br for reading. 51 00:03:00.480 --> 00:03:02.190 And you didn't see this before previously, but 52 00:03:02.190 --> 00:03:06.090 when we're writing, we now use the bin_file identifier. 53 00:03:06.090 --> 00:03:11.120 And we use a .write method that's part of that to write our bytes out in that order. 54 00:03:11.120 --> 00:03:14.070 So that's a little bit different to what you've seen before. 55 00:03:14.070 --> 00:03:18.120 And of course, the other clear thing there is the bytes method there on line three, 56 00:03:18.120 --> 00:03:23.380 we have to use that built in bytes method data in this case an integer to 57 00:03:23.380 --> 00:03:28.630 a bytes object which can be written to the binary file effectively. 58 00:03:28.630 --> 00:03:32.440 So that method on line three, the bytes method returns a bytes object. 59 00:03:32.440 --> 00:03:35.640 And that's an immutable version of a byte array, which we'll be looking at later. 60 00:03:35.640 --> 00:03:39.420 And essentially it's just a sequence of integers in the range 0 to 255, 61 00:03:39.420 --> 00:03:41.295 in other words, a sequence of bytes. 62 00:03:41.295 --> 00:03:45.055 And you can see that in the example that I've actually shown when we actually were 63 00:03:45.055 --> 00:03:47.795 in the airport, right when we'd written the program. 64 00:03:47.795 --> 00:03:51.915 Now the bites function itself doesn't quite behave as we might have expected. 65 00:03:51.915 --> 00:03:54.385 And we'll discuss line three in a bit more detail 66 00:03:54.385 --> 00:03:57.645 after we talk a bit more about the file that was created here to the right. 67 00:03:57.645 --> 00:04:01.550 So your binary file should be saved in the same directory as your Python program. 68 00:04:01.550 --> 00:04:06.260 In my case, now it's going to be there in my python program slash binary. 69 00:04:06.260 --> 00:04:09.850 So what I should be able to do is I should be able right click my file, 70 00:04:09.850 --> 00:04:12.750 click on reveal and find to go to that folder directly. 71 00:04:12.750 --> 00:04:15.800 And you can see there is the binary file at the top here, 72 00:04:15.800 --> 00:04:20.230 and we can also come back here, and the binary file should be there now. 73 00:04:20.230 --> 00:04:22.910 We can double click that IntelliJ will come up and say, 74 00:04:22.910 --> 00:04:26.830 do you wanna register a new file type, cuz it doesn't recognize this type of file. 75 00:04:26.830 --> 00:04:29.310 Clearly we're getting different results, I'm not gonna actually open that now. 76 00:04:29.310 --> 00:04:33.760 But clearly the point is that it's not a text file anymore, and it can't be read 77 00:04:33.760 --> 00:04:38.060 read a normal text file, because we're using a different process to write these. 78 00:04:38.060 --> 00:04:43.120 The very nature of binary files is often they're using non-text characters So 79 00:04:43.120 --> 00:04:46.110 again we can go back and have a look, and try to open this file. 80 00:04:48.080 --> 00:04:51.640 And you see the mechas number that is not displaying anything there at all. 81 00:04:51.640 --> 00:04:53.690 As far as its concerned it doesn't exist, so that's not good. 82 00:04:53.690 --> 00:04:56.500 So let's go back to IntelliJ and we'll double click it again. 83 00:04:56.500 --> 00:05:01.220 And this time we'll just get it to use the text file just to see what happens, 84 00:05:01.220 --> 00:05:02.520 see what will come back. 85 00:05:02.520 --> 00:05:04.990 In this case you can see, again, it wasn't very helpful. 86 00:05:04.990 --> 00:05:07.740 Basically displays a non-text character as a square. 87 00:05:07.740 --> 00:05:10.110 And we can't really do much, we can't go over. 88 00:05:10.110 --> 00:05:14.590 But essentially that's because the format of the file isn't at all that you can 89 00:05:14.590 --> 00:05:17.790 normally read from in the tradition sense that as a text file is. 90 00:05:17.790 --> 00:05:20.910 And just to confirm this is the actual output on the right hand side here. 91 00:05:20.910 --> 00:05:24.040 This is the data that got written so you can see it's in a weird format, 92 00:05:24.040 --> 00:05:25.340 the slash means it's a. 93 00:05:25.340 --> 00:05:30.230 Character code so if you look carefully you can see that we're fusing slash x for 94 00:05:30.230 --> 00:05:32.470 number zero, number one, number two, number three, 4-5-6-7-8. 95 00:05:32.470 --> 00:05:36.470 And then we're getting into hex as you can see there. 96 00:05:36.470 --> 00:05:40.840 We count through eventually to ten which is hex for the number 16. 97 00:05:40.840 --> 00:05:44.630 Other thing to point in mind is the slash t there for tap And 98 00:05:44.630 --> 00:05:49.450 that's because the number code for a byte 09 or 99 00:05:49.450 --> 00:05:53.000 character 09 is actually known as a 10. 100 00:05:53.000 --> 00:05:56.760 And for 10 that's also known as a recoratoon. 101 00:05:56.760 --> 00:05:58.950 When you want character to be more precise, 102 00:05:58.950 --> 00:06:02.160 in case you are wondering what they are and d is also missing. 103 00:06:02.160 --> 00:06:05.090 It's in fact replaced by \r And 104 00:06:05.090 --> 00:06:08.790 that's because a return character, which has got the value thrown in. 105 00:06:08.790 --> 00:06:13.160 So, what's happening is there's been some sort of conversion here. 106 00:06:13.160 --> 00:06:15.950 Intelligent knows that some of these are common codes that are used and 107 00:06:15.950 --> 00:06:19.640 its actually showing what the representation of those are when 108 00:06:19.640 --> 00:06:21.290 we're outputting this set list. 109 00:06:21.290 --> 00:06:25.320 Now, if we actually did this, if we actually output this on a Linux console 110 00:06:25.320 --> 00:06:29.720 X07 is a bell character, so, not sure whether that will work on a Mac. 111 00:06:29.720 --> 00:06:32.910 But we can go to the folder, I'm just going to my terminal. 112 00:06:34.190 --> 00:06:40.428 And if I go cd ~/Documents/Programs/PythonPrograms/Bin- 113 00:06:40.428 --> 00:06:44.150 ary, I'm going to go into that folder, and if I try and 114 00:06:44.150 --> 00:06:48.665 do a cat which is the equivalent of Of typing the file contents out. 115 00:06:48.665 --> 00:06:50.642 [SOUND] Did it again. 116 00:06:50.642 --> 00:06:52.860 [SOUND] Did you hear that beep? 117 00:06:52.860 --> 00:06:57.970 That's the beep for the character 7, XI7, which is a beep character. 118 00:06:57.970 --> 00:07:00.550 So you'll find that you'll get a beep if you run this under Linux as well, or 119 00:07:00.550 --> 00:07:02.070 if you try and type this. 120 00:07:02.070 --> 00:07:04.020 And I think you may get that on Windows as well. 121 00:07:04.020 --> 00:07:08.570 So if you go to that folder and type in the commands type and 122 00:07:08.570 --> 00:07:12.230 then space and then name in this case binary you'll probably get the same thing. 123 00:07:12.230 --> 00:07:13.830 You'll get some sort of beep for 124 00:07:13.830 --> 00:07:19.560 the character because it's the character would actually beep in Windows as well. 125 00:07:19.560 --> 00:07:21.260 So getting back to line three in our code. 126 00:07:21.260 --> 00:07:22.960 I'll close down the binary fold out now. 127 00:07:22.960 --> 00:07:24.060 And closing the run window now. 128 00:07:24.060 --> 00:07:26.190 Now getting back to line three, 129 00:07:26.190 --> 00:07:30.160 did you notice that we actually passed the list to the bytes function? 130 00:07:30.160 --> 00:07:34.360 And the reason that we passed the list, that we didn't just pass i without a list 131 00:07:34.360 --> 00:07:38.960 or not in a list It's because bite works a little strangely. 132 00:07:38.960 --> 00:07:40.340 So if you pass an integer to it, 133 00:07:40.340 --> 00:07:43.780 it creates a bite sequence with that many bites all set to zero. 134 00:07:43.780 --> 00:07:44.760 Which is not what we wanted here. 135 00:07:44.760 --> 00:07:46.920 So by enclosing I in square brackets, 136 00:07:46.920 --> 00:07:49.510 we're parsing a list with the single item I, which 137 00:07:49.510 --> 00:07:53.390 the bite function then correctly converted to a single binary file in the airport. 138 00:07:53.390 --> 00:07:56.030 If we fail to convert the single number to a bite we'll get an error. 139 00:07:56.030 --> 00:08:00.000 I can see that by changing line three And 140 00:08:00.000 --> 00:08:04.090 temporarily removing that code to convert it to a byte and also the list. 141 00:08:04.090 --> 00:08:07.350 And we try and create it like that and run it. 142 00:08:07.350 --> 00:08:10.430 We get an error, bytes-like object is required, not 'int'. 143 00:08:10.430 --> 00:08:13.120 So we do need to do that conversion before we try and 144 00:08:13.120 --> 00:08:16.850 save it as a byte in binary format to a binary file. 145 00:08:16.850 --> 00:08:21.450 Now we'll have a quick look now on how we can write numerical values greater than 146 00:08:21.450 --> 00:08:22.820 255 to a binary file. 147 00:08:22.820 --> 00:08:27.400 But first one question you might be asking now is if we have to pass a sequence type 148 00:08:27.400 --> 00:08:32.040 like list to the bytes method, and I'll just un do that so we can see it again. 149 00:08:32.040 --> 00:08:34.900 So if you need to pass a list as you saw there and 150 00:08:34.900 --> 00:08:37.250 we generated the sequence from the range function. 151 00:08:37.250 --> 00:08:38.730 The previous line. 152 00:08:38.730 --> 00:08:41.970 Whether we bother iterating through it to create a single item for each value. 153 00:08:41.970 --> 00:08:45.770 Now the answer is that we shouldn't have done that but 154 00:08:45.770 --> 00:08:49.440 really what we wanted to do was just to demonstrate what was happening. 155 00:08:49.440 --> 00:08:53.310 In practice you wouldn't create some code exactly like we've done here. 156 00:08:53.310 --> 00:08:56.670 You'd actually write the program in one less line by just adding the range to 157 00:08:56.670 --> 00:08:57.650 the bytes function. 158 00:08:57.650 --> 00:08:59.950 So in other words you do something like this. 159 00:08:59.950 --> 00:09:04.360 You get rid of the for link altogether, and 160 00:09:04.360 --> 00:09:09.750 instead of putting bytes and putting a list there, 161 00:09:09.750 --> 00:09:14.890 we put (bytes(range(17))), Like so, 162 00:09:14.890 --> 00:09:20.060 and if we run that You can see we've got exactly the same result 163 00:09:20.060 --> 00:09:22.630 in terms of output with this line of code. 164 00:09:22.630 --> 00:09:27.160 Now it'd be more useful with binary files to store the of our own numerical 165 00:09:27.160 --> 00:09:29.200 in a binary file, and we can do that. 166 00:09:29.200 --> 00:09:34.510 And it turns the itself's got a 2_bytes method that it allows it to do that. 167 00:09:34.510 --> 00:09:37.220 So this next example, what I'm going to do is 168 00:09:37.220 --> 00:09:41.480 Write the value out of four variables and allows to read those values back in again. 169 00:09:41.480 --> 00:09:43.665 Going to close this and I'm just going to that code out. 170 00:09:43.665 --> 00:09:49.240 And I'm going to we're going to set the values first. 171 00:09:49.240 --> 00:09:57.570 A equals 65534 and that's equivalent to FFFE and A equals 65. 172 00:09:57.570 --> 00:09:58.100 535. 173 00:09:58.100 --> 00:10:03.010 That's equivalent to FF FF. 174 00:10:03.010 --> 00:10:04.610 C equals 65536. 175 00:10:04.610 --> 00:10:12.310 That's equivalent to 00010000 And 176 00:10:12.310 --> 00:10:18.050 finally D equals 2998302, that's equivalent to O2, so 177 00:10:18.050 --> 00:10:23.440 2DCO1E, so that's our four variables, 178 00:10:23.440 --> 00:10:27.078 and I'm just giving you the hex equivalence there to the right hand side, 179 00:10:27.078 --> 00:10:33.990 And this the code so we can put right, so I can put width, open two. 180 00:10:33.990 --> 00:10:38.920 And b w because we want to write a binary file format. 181 00:10:38.920 --> 00:10:39.580 S bin file. 182 00:10:41.690 --> 00:10:43.960 I'm going to put bin underscore file dot right. 183 00:10:45.240 --> 00:10:50.180 A dot two underscore bye method we converted the integer 184 00:10:50.180 --> 00:10:53.210 Automatically two bytes and then we're gonna pass an extra parameter here, 185 00:10:53.210 --> 00:10:56.520 we're gonna put the length, and we're gonna put the bar [INAUDIBLE] and 186 00:10:56.520 --> 00:10:57.600 we'll talk about that shortly. 187 00:10:57.600 --> 00:10:58.850 I'm gonna set that to big. 188 00:11:00.940 --> 00:11:03.590 List D we'll set that to X to just be a little bit different because there's 189 00:11:03.590 --> 00:11:04.430 a reason for that shortly. 190 00:11:04.430 --> 00:11:06.210 We'll just do the same for the other two. 191 00:11:07.460 --> 00:11:08.620 In fact on three variables. 192 00:11:08.620 --> 00:11:12.810 The next one is going to be two bytes again, so that's going to be B. 193 00:11:12.810 --> 00:11:16.780 We're basing that on the fact that this representation over here, 194 00:11:16.780 --> 00:11:19.690 so for A and B we only needed two bytes to store the value. 195 00:11:19.690 --> 00:11:22.360 But the next one, because we've clocked over the 65 power 53 mark, 196 00:11:22.360 --> 00:11:24.220 we need to use four bytes. 197 00:11:24.220 --> 00:11:25.740 So I'm going to set that to four. 198 00:11:29.580 --> 00:11:35.160 Four and the last one which will be X that also needs four bytes as you can see, 199 00:11:35.160 --> 00:11:36.640 so I'm going to change that to four. 200 00:11:36.640 --> 00:11:40.670 And I'm going to do something extra, and then we'll discuss it. 201 00:11:40.670 --> 00:11:45.570 So I'm going to set that to write another variable out, X again, this time I'm going 202 00:11:45.570 --> 00:11:50.770 to leave it on four, but I'm going to change this to little like so. 203 00:11:50.770 --> 00:11:51.700 So first of all, run it. 204 00:11:53.700 --> 00:11:56.800 And whoops, I made a typo there, so that should've been a, b, c. 205 00:11:56.800 --> 00:11:59.495 And I'll run it again. 206 00:11:59.495 --> 00:12:02.710 [LAUGH] I having a hard time typing at the moment. 207 00:12:02.710 --> 00:12:05.130 little, that's meant to be, so I will try that again. 208 00:12:05.130 --> 00:12:07.210 Third time lucky, run it. 209 00:12:07.210 --> 00:12:10.990 And we've got no output, which means that, well no errors, which is a good start, 210 00:12:10.990 --> 00:12:13.290 and we haven't got any output in our program. 211 00:12:13.290 --> 00:12:16.610 So it's surprising that we have anything showing on the screen. 212 00:12:16.610 --> 00:12:19.145 So for these two parameters and 213 00:12:19.145 --> 00:12:21.600 it's called bye method the first one is the number of bytes we want and 214 00:12:21.600 --> 00:12:25.010 the second is whether to return the result as big indian or little indian. 215 00:12:25.010 --> 00:12:27.460 Now the number of bytes is straight forward. 216 00:12:27.460 --> 00:12:30.690 Cuz if you remember from the earlier lecture on binary the largest number 217 00:12:30.690 --> 00:12:34.270 we can store in two bytes or 16 bits in other words was 65535. 218 00:12:34.270 --> 00:12:37.870 As soon as we get to a higher number we need to go to more bytes to store 219 00:12:37.870 --> 00:12:38.850 those numbers. 220 00:12:38.850 --> 00:12:40.080 And it's in this case. 221 00:12:40.080 --> 00:12:44.350 Case for c, we need at least 3 bytes to represent that variable. 222 00:12:44.350 --> 00:12:48.030 But it's usual when you're writing binary to use an even number of bytes, and so 223 00:12:48.030 --> 00:12:51.180 that's why we've converted it up to 4 in that case. 224 00:12:51.180 --> 00:12:56.060 Now ending this are rows, this is that And here the second parameter so 225 00:12:56.060 --> 00:12:59.960 this indian row where computer manufacturers had to make a decision 226 00:12:59.960 --> 00:13:02.400 about how to store numbers in their computers memory. 227 00:13:02.400 --> 00:13:06.980 So what happens is stores the most significant byte first 228 00:13:06.980 --> 00:13:09.630 with the remaining bytes of the number following in order, 229 00:13:09.630 --> 00:13:12.080 the same way you'd write it in other words. 230 00:13:12.080 --> 00:13:14.930 But little indian is the reverse of this in other words it shows 231 00:13:14.930 --> 00:13:17.810 the least significant byte being stored first. 232 00:13:17.810 --> 00:13:20.420 Now the initial choice was largely arbitrary. 233 00:13:20.420 --> 00:13:24.050 Whilst for example, IBM choosing big endian for their mainframes. 234 00:13:24.050 --> 00:13:25.080 This is years ago. 235 00:13:25.080 --> 00:13:29.580 And Intel, Intel CPUs and Intel the manufacturer are using little endian for 236 00:13:29.580 --> 00:13:31.340 their early microprocessors. 237 00:13:31.340 --> 00:13:33.900 So once the choice has been made, it continues to be used and 238 00:13:33.900 --> 00:13:35.830 maintain backwards compatibility. 239 00:13:35.830 --> 00:13:39.560 Now we've chosen in this example Beaconed in for all but the last right. 240 00:13:39.560 --> 00:13:43.860 And as you can see there by passing big for the first four calls 241 00:13:43.860 --> 00:13:47.446 we used big [INAUDIBLE] and the landing difference is the fifth one, but 242 00:13:47.446 --> 00:13:50.720 we've written a value of x again, and we've actually used little indian form. 243 00:13:50.720 --> 00:13:51.400 Comment for that time. 244 00:13:51.400 --> 00:13:54.040 What we're gonna do is have a quick look at reading the numbers back into our 245 00:13:54.040 --> 00:13:58.330 program, before we move on to more pythonic ways of storing number variable 246 00:13:58.330 --> 00:13:59.380 to a binary file. 247 00:13:59.380 --> 00:14:00.070 So that's the write. 248 00:14:00.070 --> 00:14:03.190 Let's write some code now that's going to read those values back. 249 00:14:03.190 --> 00:14:07.890 So I'm gonna put with open, binary2. 250 00:14:07.890 --> 00:14:13.230 We're gonna do a br mode to read at this time. 251 00:14:13.230 --> 00:14:14.750 And we're gonna call it binFile. 252 00:14:14.750 --> 00:14:15.590 Let's use a different name. 253 00:14:15.590 --> 00:14:16.680 We don't really have to, but I will. 254 00:14:16.680 --> 00:14:21.970 And what we'll do is, we'll assign some new variables, e, f, g, h, i. 255 00:14:21.970 --> 00:14:25.140 Five variables for the five that are now in that file. 256 00:14:25.140 --> 00:14:30.570 So we can put e = int dot from underscore bots and 257 00:14:30.570 --> 00:14:33.750 it's the opposite to two underscore bots from is converting it from the file. 258 00:14:33.750 --> 00:14:37.630 We are going to put bin underscore file, or bin file. 259 00:14:37.630 --> 00:14:39.670 Just to show you that we can't do it that way, 260 00:14:39.670 --> 00:14:43.916 we are going to use Use the same identifier so we'll call that file. 261 00:14:43.916 --> 00:14:47.010 Because there isn't a reason now that we're finished with the first 262 00:14:48.369 --> 00:14:49.671 that we can't reuse that. 263 00:14:49.671 --> 00:14:52.880 So equals it dot front byte bin file and 264 00:14:52.880 --> 00:14:56.830 then you need to pass the number of bytes, in this case two. 265 00:14:56.830 --> 00:15:00.313 Two and then the format whether it's big or little, we're going to put b like so. 266 00:15:00.313 --> 00:15:05.860 So that's creating a variable called and we'll do the same for F, g, h, i. 267 00:15:05.860 --> 00:15:10.840 So it's f, g, h, i. 268 00:15:10.840 --> 00:15:16.850 And of course, that represents, or relates to e is variable a, 269 00:15:16.850 --> 00:15:22.770 f is b, g is c, h is x but with big-endian format, 270 00:15:22.770 --> 00:15:27.360 and i is that same variable x again in little-endian format. 271 00:15:27.360 --> 00:15:30.210 So for the last one, we've purposely left it as big again. 272 00:15:30.210 --> 00:15:33.170 So we're reading them all back in big format again. 273 00:15:33.170 --> 00:15:35.930 The other thing I need to do is change the last three there to make 274 00:15:35.930 --> 00:15:40.270 sure we're storing the correct number of, or reading the correct number of bytes. 275 00:15:40.270 --> 00:15:44.910 And relates completely to the number that we used when we created the binary, So 276 00:15:44.910 --> 00:15:47.960 if we run that again now, actually what I'll obviously need to do first, 277 00:15:47.960 --> 00:15:49.840 is preprint it. 278 00:15:49.840 --> 00:15:53.930 Print a, you can sort of see the result and confirm they're actually the same, 279 00:15:53.930 --> 00:15:58.090 and we'll just change all those now. 280 00:15:58.090 --> 00:16:02.710 So F G H I. 281 00:16:02.710 --> 00:16:07.740 So now if we run that And I missed a method didn't I? 282 00:16:07.740 --> 00:16:12.260 I should have put .read because I specified the name of 283 00:16:12.260 --> 00:16:16.150 the file identifier but I didn't put .read so 284 00:16:16.150 --> 00:16:21.780 we need to actually call the method .read To do the opposite of .write and 285 00:16:21.780 --> 00:16:25.160 to read the data from, of course, the binary file that we've created. 286 00:16:28.480 --> 00:16:30.100 Okay, so we have a .read for all of those. 287 00:16:31.610 --> 00:16:34.100 Now, try again, run it again. 288 00:16:34.100 --> 00:16:35.480 And this time we've got some results. 289 00:16:35.480 --> 00:16:39.550 And you can see that the first four examples that have come back. 290 00:16:39.550 --> 00:16:40.950 This is E, F, G and H. 291 00:16:40.950 --> 00:16:43.050 You can see they should represent, 292 00:16:43.050 --> 00:16:46.170 well they do represent the exact numbers that we originally saved them in. 293 00:16:46.170 --> 00:16:50.250 But the fifth one is what we want you to have a look at. 294 00:16:50.250 --> 00:16:55.820 Notice how that number is now completely different to the X value or 295 00:16:55.820 --> 00:16:57.790 2998302 that's on line 11. 296 00:16:57.790 --> 00:17:00.520 And what's happened there is, I'll just close this down so 297 00:17:00.520 --> 00:17:01.890 you can see it a bit easier. 298 00:17:01.890 --> 00:17:06.100 We used big format again here, so we saved it in little Indian format and 299 00:17:06.100 --> 00:17:08.910 we tried to read it back again in big Indian format. 300 00:17:08.910 --> 00:17:12.620 So in other words the bytes are in the reverse order because they were written 301 00:17:12.620 --> 00:17:15.550 Initially in little indian format on line 18 and 302 00:17:15.550 --> 00:17:19.140 we're trying to read them back in big indian format in line 29. 303 00:17:19.140 --> 00:17:23.270 This demonstrates that's very important to understand the structure 304 00:17:23.270 --> 00:17:27.570 of the binary data in a file if you're going to attempt to read it successfully. 305 00:17:27.570 --> 00:17:31.260 Now, again it's one making point here that this is pretty low level stuff 306 00:17:31.260 --> 00:17:34.270 They can come out of this knowing that it is possible, but it's a bit fiddly if all 307 00:17:34.270 --> 00:17:37.690 you want to do is store your programs data and read it back in again. 308 00:17:37.690 --> 00:17:38.270 But luckily, 309 00:17:38.270 --> 00:17:43.320 with that said Python provides a couple of other methods with dealing with this data. 310 00:17:43.320 --> 00:17:46.690 And to make things easier for you to do something along these lines. 311 00:17:46.690 --> 00:17:48.670 So we'll move on now and look at pickle and 312 00:17:48.670 --> 00:17:52.420 shelf, which are two ways of working with binary data in the next video