In this video, Kathryn teaches you how to manipulate .zip files using Python's zipfile module. Learn how to access the metadata of a .zip file, as well as read the contents of each file contained within the .zip. Finally, learn how to extract .zip files using Python.
- [Instructor] Before we use the Zipfile Module let's create some files and then zip them up in a zipfile. We're going to create two files and the first one that we're going to create is going to be called "purchased.txt", and inside of here, we'll print a couple things, say things you've purchased are apples, towels, and candy. These are things that you've purchased in the past week. And then we're going to create another file and it is going to be called "wishlist.txt".
And again, this can hold any type of data, but say the thing that is on your wishlist is an iPhone. And so now that we have these two files set up for us, we're going to go into the Finder and zip them up. And so here are the two files and we'll go ahead and select them both, and then just compress the two items, and now they're both in this archive here. And now to make sure that these two files don't mess with anything else, we're going to go ahead and just put them somewhere else where we don't have to deal with them.
Back in the code here, we can go ahead an import the Zipfile Module, and then get access to this zipfile that we just created called archive. And so to do this, we'll create a variable called "zip" and this will represent our zipfile in this case, and we'll access the Zipfile Module, and then we'll access the Zipfile Constructor and so this will construct a zipfile fore us, and all we'll have to do is put in the name of the file here and so it was called "Archive.zip", and then the mode that we are going to open this in is just the reading mode, similar to the file modes that we dealt with in a previous video.
With access to our zipfile, we can then list everything that's inside of this zip folder. And a zipfile and a zip folder, those are both just the same thing. We'll go "zip.namelist" and this will list everything that's in that zip folder, and we'll just print this out so we can see it. And there we go. We have our "purchased.txt" and we have our "wishlist.txt", and these are both in our archive, but notice in our Finder, they're not shown. All you see that there is a zip folder and it's called "Archive".
So with the Zipfile Module, you kind of get a little bit of behind the scenes and what's in that zipfile. We can also go through the different meta data that's in this zip folder, this zipfile. And to do that, we can use a for loop, and so we'll say "for meta in zip.infolist", and so this info list is just a list of the meta data that's in there, and then meta is going to be each item that's inside of that info list. And for each meta, we're just going to go ahead and print it out.
Running this, we see that there's two items. We have the meta data for "Purchased" and we have the meta data for "Wish list". And so you see that they're text files. You see the compression type, you see what has access to different modes, you see a bunch of other meta data that is associated with these files. And this might be useful to you in a certain application. And you could also get meta data for specific files. And so you could go "info = zip.getingo". So getting the information, getting the meta data about say, "purchased.txt".
And so if we print this out and run it, we see that we again get the "purchased.txt" meta data. We could also get the actual data that is in these files in the zip folder. To do this, we can just go "print(zip.read("wishlist.txt"))". And so if we print that, we see oh, iPhone is there. And notice there's a "b" here, meaning it's stored as a bytes object.
We can also get access to the file itself by going "with zip.open('wishlist.txt')" and this is going to open the file that's inside of this zip folder. What file is going to open? The "Wishlist" file. And so we're going to say, if converting wishlist.txt to a file is okay, then we're going to go ahead and read it as this f file. And so "f" gets the value of what this zip.open returns. And then we read it, and then we print this, and there we go.
We get the iPhone again. But there's a lot more you can do with it because you'd get access to this as a file f. We can also extract files from the zipfile in python code. And to do this, we can just go "zip.extract", and we can just put the one file that we want to extract from the zipfile, and so say, it's "purchased.txt" that's what we want to extract. We can go ahead and run this. And if we go back to the Finder, we'll see that "Purchased.txt" has been extracted from the archive.zip folder.
And then we'll go ahead and move this over here somewhere where we don't have to deal with it. We'll just replace it, not a huge deal. And then we can go back to the code. We'll comment this out, and we can actually extract everything that's in that zip folder by going, "zip.extractall" just like that, and this will extract everything that is in that zip folder. We run this, go back to our Finder, and there we have the two items as well. Now we'll go back to the code here and just "zip.close" this file so that way it's all neat and tidy and everything is closed for us.
Knowing about the Zipfile Module can be very helpful if you're working with zipfiles in your project.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content