Creating and Extracting Zip Files using Python

How to zip, unzip, create and extract files with Python

Vighnesh Gawad
Python in Plain English

--

Photo by Jefferson Santos on Unsplash

In this blog post, we are going to learn how to unzip and zip files using Python. It will be useful to learn if you want to extract a zip file without using external software or want to automate the process for multiple zip files.

To do this we will use zipfile module which is inbuilt in Python. So just we have to import it in our scripts like this.

import zipfile

Extracting or unzipping from a zip file

The first step is to open the file. We can do that using ZipFile class. We passed the first argument to ZipFile class a filename with the path that you want to extract, if that file is in the current folder in which the Python script is running then the only filename is enough. Then we passed the second argument ‘r’ as we want to open a file using read permission that we want to extract it.

myzip = zipfile.ZipFile('file.zip','r')

If we want all of its content from zip file name file.zip. We will use extractall() function and passed folder name which will be created in the current folder and all files will be extracted in that folder.

myzip.extractall('Foldername')

Or if you want to extract one specific file from file.zip then for that we use extract() function and passed filename of that specific file. This will extract that file in the current directory.

myzip.extract('filename.txt')

At last step we close file using close() function.

myzip.close()

It is important to close the file as it is likely damage or corrupt file if you leave it open. If you don’t want to use open and close file every time. You can use with context manager to open a file which will automatically close the file after execution. It will also reduce the number of lines.

with zipfile.ZipFile('file.zip','r') as myzip:
myzip.extractall('Foldername')

If a zip file has a password, then you have to pass the password to setpassword() function before extracting or you could pass it toextract() and extractall() function with pwd argument. As function needs password in byte string either you have to pass it as b'password or as str.encode('password').

with zipfile.ZipFile('file.zip','r') as myzip:
myzip.setpassword(b'password')
myzip.extractall('Foldername')

Or you can do this.

with zipfile.ZipFile('file.zip','r') as myzip:
myzip.extractall('Foldername',pwd=b'password')

Creating a zip archive file

Now creating a zip archive file will be kind of similar to extracting but here we need to open the file in write mode. We will use ‘w’ as the second argument to open files in write mode. And then use write() function in which we passed the name of a file we want to write the file to a zip file.

with zipfile.ZipFile('file.zip','w') as myzip:
myzip.write('file1.txt')

To add multiple files we would add multiple write statements with different filenames just like below.

with zipfile.ZipFile('file.zip','w') as myzip:
myzip.write('file1.txt')
myzip.write('file2.png')

Unfortunately, using zipfile module you can only extract the zip file with a password but can’t create an encrypted zip file with a password. So that’s it for this post, let me know if you liked this article or have any questions.

Conclusion

Thanks for reading. To know more about zipfile module and its options you can check out their docs.

--

--