 Hey everybody this is Brian and welcome to the 16th Python tutorial. Today we're going to be covering binary files. Originally I was going to split this up into two videos but I think I'm just going to do one just to make it a little faster. If you're wondering what a binary file is, it's a computer file that is not a text file. That's the very descriptive Wikipedia article right here. What does that mean exactly? Well, a text file is meant for humans to read and write. I'm sure you've opened like g-edit or notepad or you know text on Mac or whatever and you've just written yourself a quick note saved it. Well, if you examine the contents of that file it looks exactly how you typed it. That's a text file. A binary file would be like well an image or a database or this web browser, any executable or any type of file you can imagine. Here's an example of what it might look like in a hex editor. You can see there are hex values and it gets pretty complicated and there's some computer classes you should probably take like you should learn like what a bit and what a byte is and what a nibble and all that stuff but we're not going to really cover that because I don't want to bore you with the details and if you're watching this video you probably already know what they are so if not you can go out and just learn on your own. Google's awesome like that. We're going to call this the very descriptive video 16 and we'll call this binary files. So why would you want a binary file? Well let's say we have a list and in that list we have some numbers let's just say 12 34 34 34 say 200 and 255 that's our list of numbers and we want to write those numbers to a file and we want to be able to read those numbers back exactly as they are. Well we're gonna make our string literal here and I know the path to my directory. I'm going to name this test.txt now I know somebody out there is like whoa hold the phone here test.txt isn't that a text file actually no the extension does not denote what type of file it is it's just a name the contents of the file are well up to you typically you'll get what's called a header inside of a file and let's just for whatever reasons say this is the header so if you ever open like a JPEG or a bitmap or something in in like notepad you'll actually see the header it'll say like you know image-jpg or whatever that denotes what type of file it is so anyways we're gonna make a buffer we'll call this we're gonna call the bytes function what this does is it turns our list into well a list of bytes so we can actually print let's run this and see what happens here you can see our list looks nothing like what we have up here it's B and then this little guy and then slash XOC what's going on here well what it's doing is converting it to bytes in memory hence the little B notation in front of our string that says this is a byte string instead of a literal string or a real string and this is hex if you don't know what hex is there's there's a whole science behind it I won't get into it but it's just a different way of displaying the number like X F F that's 255 so you can see it gets kind of interesting now let's actually take this and we're gonna write this to a file with we're gonna say open and we're gonna say binary write very important that you say binary write if you don't you may encounter an error now we're gonna write that buffer out to the disk run this again process finish let's go look at this little guy here and if I just drag it over here you see it's got this gobbly gook what is that well that is the in my case g edit your yours might look drastically different depending on what you're really looking at it's gonna based on character encoding well I was trying to get a change but it didn't but anyways it may display it differently it may show like a little happy face or like a little why with two dots over it or whatever but that is an example of a binary file you're not meant to read this as a human being this is strictly for the computer so if you've never seen a binary file congratulations that's your first and now we're going to actually read it back and this is why I was saying I'm just gonna do it in one video because this is originally going to be two videos and what's the point so let's actually just print gonna read it back and let's say with and while I'm typing this I'm just gonna open this with binary read but while I'm typing this be sure to check out the the void realms Facebook group there's over 200 of us in there and we just try to help each other out so let's go buffer equal oops and we want to read you can read all of it we're gonna read a maximum of 16 bytes if you just do read I think it's just gonna read all of it but I just wanted to demonstrate that you can limit how much you're gonna read back and now we're going to print see not doing good today my brain is just not here I had a pretty long day at work I'm sure you guys know how that is so we're going to take that buffer now you may be saying well don't you have buffer up here actually I do but I'm changing the variable because I'm reading it I'm replacing it in memory it's a not a popular programming style but it's actually a very common one for I am buffer now I'm just gonna loop and I'm going to print out and we're gonna do what's called a cast if you're not familiar with the cast I think we've covered it in other videos it's just converting one data type to another so we're just converting it into an integer and making sure that it's going to display correctly let's run this and see what happens so you see the length of the buffer is 4 we've read the file notice how we said read 16 if it was longer than 16 it would chop it off at 16 and you'd only get 16 so if that file had 300 bytes in it we'd only get 16 because we said limit it and the length is 4 so we know we got 4 and sure enough our values are 12 34 255 now why would you want to write a binary file simply put you can pack a lot of information into a binary file that you couldn't with a text file for example think of a picture you've got like a 600 by 600 picture well you know take 600 by 600 that's how many pixels are in there takes even more bytes to represent that picture because you've got different color scales you've got the size of the image the header I mean all this information and it actually gets into what's called a structure which we're going to cover in the next tutorial which is a very common theme for binary files well that's it for this tutorial pretty painless be sure to visit my website void realms comm for the source code for this and other programs and go out to Facebook and visit the void realms Facebook group