Working with ZIP archives in Python

Working with ZIP archives in Python is essential to import and extract data from bulky files compressed in a single folder. Learn more about it here.

ZIP archives in Python: Examining Zipfile Contents

There are a few ways to inspect the contents of a zipfile. You can use the printdir to just get a variety of information sent to stdout

with zipfile.ZipFile(filename) as zip:


File Name Modified Size

pyexpat.pyd 2016-06-25 22:13:34 157336
python.exe 2016-06-25 22:13:34 39576
python3.dll 2016-06-25 22:13:34 51864
python35.dll 2016-06-25 22:13:34 3127960


We can also get a list of filenames with the namelist method. Here, we simply print the list:

with zipfile.ZipFile(filename) as zip:

Out: [‘pyexpat.pyd’, ‘python.exe’, ‘python3.dll’, ‘python35.dll’, … etc. …]

Instead of namelist, we can call the infolist method, which returns a list of ZipInfo objects, which contain additional information about each file, for instance a timestamp and file size:

with zipfile.ZipFile(filename) as zip:
info = zip.infolist()
Out: pyexpat.pyd
Out: (2016, 6, 25, 22, 13, 34)
Out: 157336

ZIP archives in Python: Opening Zip Files

To start, import the zipfile module, and set the filename.

import zipfile
filename = ''

Working with zip archives is very similar to working with files, you create the object by opening the zipfile, which lets you work on it before closing the file up again.

zip = zipfile.ZipFile(filename)

In Python 2.7 and in Python 3 versions higher than 3.2, we can use the with context manager. We open the file in “read” mode, and then print a list of filenames:

with zipfile.ZipFile(filename, 'r') as z:

Extracting zip file contents to a directory

Extract all file contents of a zip file

import zipfile
with zipfile.ZipFile('','r') as zfile:

If you want extract single files use extract method, it takes name list and path as input parameter

import zipfile
for cont in zfile.namelist():

ZIP archives in Python: Creating new archives

To create new archive open zipfile with write mode.

import zipfile

To add files to this archive use write() method.

new_arch.write(‘filename.txt’,’filename_in_archive.txt’) #first parameter is filename and second parameter is filename in archive by default filename will be taken if not provided new_arch.close()

If you want to write string of bytes into the archive you can use writestr() method.

str_bytes="string buffer"

Learn more

Leave a Comment