 Hey everybody, it's Brian. Welcome to the 17th Python tutorial. It kind of screwed me up my notes Or we smushed the last two together into one video So it kind of screwed me up a little bit here, but today we're going to be talking about binary files and structures and To kind of help me with that we're going to look up on Wikipedia the JPEG file format You're all familiar with pictures out on the internet like this cute cat. Everybody knows I love cats so What really makes this file? I mean, how do we get that image on the screen? Well, you use what's called a structure and I use JPEG on purpose because it's a very complex Data structure and if you just kind of scroll down here, you don't have to read all this But just understand remember how we talked about, you know, hex FF is 255 in the last tutorial That's the start of the image. So you have this nice structure here to work with This is the common MPEG marker And what this is is just simply a data structure. You'll hear this in different languages You'll hear it called a head a header a struct a structure In Python, they're just structs But they're very easy to work with in Python and they're very painful to work with in some other languages Now, what would you use a struct for? You can see how this is just to note something. It's like the the size the progressive whether it's got Huffman tables quantitative or quantitative Quantization tables to find restart intervals scans. I don't expect you to know any of this. What I'm getting at here is JPEGs They're actually very complex You'll have this eight by eight sub image. That's right sub image meaning a picture can actually be made up of other pictures And those are structures that are read. You got it from a binary file Now you start to understand a little bit more about why binary files exist You can pack a lot of data into them I almost say like 10 times the amount of data you couldn't a simple text file if you try to describe something and You can have these beautiful structures, which are very easy to work with so we're gonna actually Not mess around with images because it's a little too complex for us right now But we're going to make our own structure. So let's actually go in here go new Did I call it video 18? I did this video 17 my bad Like I said my notes just totally video 17. There we go. We'll call this struct now first thing we want to do is I'm going to link Link cheese put a link in there for when you download my tutorial off my website that you can very quickly and go out and Read about this, but we're going to from struct Import star. What's that? What that's really doing is we're taking the struct and we're importing everything inside of it that struck package We've done this before so And we're going to make a string literal here, which is of course the path to our file And yours will be whatever, you know see my documents, etc. Etc. A little quick rant. It just bothers me I mean I used to love windows and I got a Mac and I got into the Linux and I just It just bothers me that windows is different than everything else in the planet It's like why can't everything just be the same? so with a structure We need a format What does that mean? Means you need to understand what you're putting into that structure You don't just willy-nilly start throwing bits in there. I mean crazy things will happen, right? So with a structure you have two concepts packed and unpacked Pack is when you're packing it down to store it into a file or like into a box think of it that way You're going to package it and unpack you well you guessed it You're pulling it back out of that file and you're going to do something with it So I'm gonna say packed equals Pack and this is from the struct package Now first thing you need is the format Then you need some data You notice how there's a star there star values what that means is it accepts one or more We can just go infinity and beyond so if you ever see star values or star something you know that you can have multiple in there So we're just gonna say one two three Why not three point one four So that's our structure Integer integer decimal. That's how that reads so integer integer decimal That way the struct package Particularly this pack function knows exactly what we're storing. We're giving it a format to work with If we gave it the incorrect format, it's gonna start squawking you're gonna get all sorts of errors and it's gonna get ugly This is a pretty Dumb-down example of struct pack you can find some very complex examples out on the internet But I really wanted to dumb it down just so we understand what's going on here Now we've got our packed structure here. So let's just print this out Just to see what it looks like I'm a big fan of show-and-tell and you can see there's our structure here Holy moly. Look at that That is a whole lot what's going on there Well, if you paid attention during your computer class, you know that an integer at least on my platform is four bytes so there's one two and 3.14 is right there So that's what's going on is the packed function turns this into a binary string That's you know, essentially Python's gonna push it down onto the desk So we're gonna say print Writing file there put that there just because I'm picky like that With open str file and you guessed it. We're going to binary write as F now What we're going to do here is We're going to write this out And we're going to say F dot write Bites because we need to convert this into you guessed it bites Packed and what that does is it takes our binary string and Converts it into you guessed it bites and then writes it down to the desk now. We're going to print Reading the file Whoopsie, there we go reading the file Or say with open and you guessed it We're going to binary read I'm going to Europe this summer. So I'm kind of nervous about it. I'm gonna go to France specifically Paris Italy Spain And I don't know where else we're going. It's kind of surprised. We'll find out But I'll be gone for two weeks. So it's going to be interesting So we're going to just read Now you notice how from the last tour we put a 16 in here this time We're not doing that. We're just going to read everything the entire contents of the file in there Now if you knew the length of your structure, you would actually say only read a certain amount For this example, I'm just going to read everything And we're going to say unpacked Equal and you guessed it. We're going to unpack that need to format and we're going to give it the buffer and then we're just going to print Probably help if I actually like did something with that. There we go Whoo, let's see if this thing runs. All right, so We've written the file. We're reading it back and we've sure enough we get you guessed it one two and three point one four Nice Let's see what that looks like out on the disc And there is our beautiful file in all its glory. It may look different on your screen, but that is the just of it all So that is a struct now you know the basics of why you would use a struct now You can think of certain projects that you would do like if you wanted to store like Somebody's employee ID and their age or something like that. You'd make a nice structure and you could you know pump these out to the file That's all for this tutorial. I hope you found this educational and entertaining Thank you for watching and Be sure to visit my website void realms.com for the source code for this and other tutorials