00:00 In this lesson, I’m going to show you how to manipulate archive files like ZIP and TAR files. These files are convenient because they package multiple files into one, often compressed, version of that multiple file set.
00:16 They can be very helpful, especially when you need to save space, or send files over a network, or something like that. The rest of this lesson will kind of be organized into a two-part sequence.
make all of this super easy, but remove a little bit of that granular control. So, as I said,
zipfile.ZipFile() makes working with ZIP files almost like working with any other file, and then they have
.extract_all() functions to get those files back out of the ZIP.
unpack_archive() functions are pretty much just—give this a bunch of files and it will make them into a ZIP file, or a TAR file, or whatever you want, and then give this the archive and it will extract it all for you.
01:36 So kind of, like, the hard, but more granular approach versus the easy, but a little bit less flexible version. For the tutorial I’ll use a super-simple sample directory that I’ll then just recombine, and zip and tar in multiple ways, and just start making a whole bunch of crazy stuff happen there.
And then I’ll show you how the
zipfile.ZipFile() constructor works. You just give it the name that you want your ZIP file to have and then you give it a file mode just like you would with
with open() as.
And then if I use my
listdir() here, I can see that
first.zip has indeed been created. If I want to actually take a look at the things that are in that ZIP file, I can also use the
ZipFile() constructor, I just have to now pass in the read mode parameter.
03:08 It doesn’t have anything in it. So, that’s all pretty cool, pretty easy to add. And you can do this same thing with a loop if you want to add all of these items in here, and it works just fine with directories as well.
You just need to pass
z.write() a file or directory name, and it will write it into the ZIP file. So, that’s all pretty convenient and I’ll just use
os.listdir() one more time to show you that opening it in read mode doesn’t actually do anything to the ZIP, it just gives you access to the data that’s inside it.
zipfile will actually do all this work for me to create this extracted directory and then put the extracted file into it. So if I just
listdir('extracted'), you can see that that’s in there, just as expected.
04:24 So now, let me quickly—I’m going to create a new ZIP file, which is going to be a ZIP file with all of the things in here in it. So it’ll show you that you can actually create nested ZIP files, you can have directories in your ZIP files.
z.write(f), there we go. So now, if I take a look, I’ll do just what I did above, except I will say here—I’m going to change this call from
.extract_all(), and I’m going to say
'extracted_all/', and so that will be the new directory, which will have all of this extracted content.
.extract_all() is great. It actually has some other parameters, too, like it has a password parameter,
pwd, so if you’re extracting from a password-protected ZIP file, that’ll work just fine.
It has a lot of other parameters, as well, that you can use. Now, I
listdir(), you can see I have this
'extracted_all' directory, and then I will list that and show you that it has everything that was in this
I won’t give it a
root_dir, because it’s just going to default to zipping everything in my current directory. So I can take that, and now I can say
shutil—and, of course, what I forgot when I was making this name was that
shutil is a little smarter than
zipfile, so it actually appends the
.zip on, without me even needing to.
So I now have a
made_with_shutil.zip.zip, which is kind of silly but works just fine as a ZIP file, so it’s not a big deal. And then I can also say
shutil, and I’ll show you when I unpack it that it has all the correct stuff in it.
This is all starting to get a little bit hairy with all of these different ZIP files and everything. I hope that you can keep track of it just fine. And so now, I can say
os.listdir() for, hopefully, the final time in this lesson.
You can also use
shutil.make_archive() to make a TAR archive, which I haven’t talked much about, mostly because the
zipfile module has a corresponding
tarfile module that has a lot of the same syntax.
But you can use
shutil.make_archive() to make a TAR file, which works just as well. Then you can also unpack with the same logic. So, this all works great, and
shutil makes things super simple and easy, but again, you lose a little bit of the granularity.
So, that’s something to keep in mind. In the next and last lesson, I’m going to discuss how you can use the
fileinput module to put multiple files together into one input stream, which can make reading from a large number of related files quite easy.
Become a Member to join the conversation.