/ python

Tuesday Tooling: Working with ZIP files in Python

ZIP archives, those files that are full of squashed files. We've all used them, but have we used them with Python?

So what is it?

ZipFile is a Python module that enables us to work with ZIP archives in Python. I discovered this module thanks to a colleague who was working on a project to extract over 100 ZIP files at once. To do this manually would take ages, say 10-15 seconds per file. But with ZipFile it all happened in a matter of seconds and so this post was born!

So how do I install it?

Good news! You already have it installed!

Animated-GIF-downsized_large--4-

Hi reader!

I never put my blog posts behind paywall or pop ups because quite frankly that is annoying and prevents anyone from accessing the content. I will always keep my blog content free of charge. But I do ask that if you are able and willing, that you buy me a "coffee" as it helps me to pay for hosting this blog, and to buy stuff to hack from Poundshops / Dollar Stores / Aliexpress which are used in free projects and reviews on this blog. It is You dear reader who make this possible, and I am immensely grateful for your support.
Thanks!

So how do I use it?

Lets take a look via a few projects.

Create a ZIP

Screenshot-from-2020-05-12-18-32-37
I want to create a ZIP file called spam.zip which will contain the file eggs.txt which is already in the directory.

from zipfile import ZipFile

I import the ZipFile module.

with ZipFile('spam.zip','w') as myzip:

Create an object / file called myzip and set it so that I can write to a file called spam.zip which is the ZIP archive that I wish to create.

    myzip.write('eggs.txt')

Write the file eggs.txt to the ZIP archive using the object myzip reference.

Read the contents of a ZIP

Screenshot-from-2020-05-12-18-43-10
I've got the same ZIP file spam.zip and I want to see what is inside it. Using the code below I can see the contents of spam.zip as a Python list.

from zipfile import ZipFile

I import the ZipFile module.

zf = ZipFile('spam.zip', 'r')

Create an object called zf which will store the contents of the ZIP archive.

print(zf.namelist())

Print the contents of the ZIP archive spam.zip as a Python list.

Extract a ZIP

Screenshot-from-2020-05-12-18-54-58
The same ZIP file spam.zip is used and I want to extract the files from within.

from zipfile import ZipFile

I import the ZipFile module.

archive = ZipFile('spam.zip')

Create an object called archive that will enable me to work with the ZIP file.

archive.extractall()

Extract the contents to the current working directory.

archive.close()

Close my work with the archive.

What is the code for the example at the top of the page?

Here you go! I mashed the read contents and extract a ZIP together.

from zipfile import ZipFile

I import the ZipFile module.

zf = ZipFile('blogpost.zip', 'r')

Create an object zf that I use to access the ZIP archive.

print("I found these files in the archive...")

Print a message to the user.

print(*zf.namelist(),sep='\n')

Print the contents of the list to the screen, with each item in the list on its own line.

zf.extractall()

Extract the files to the current working directory.

zf.close()

Close my work with the archive.

Happy Hacking!