Extract Files from Zip Archive to Folder – Unzip in Python

This Python code will extract or unzip all the files in a Zip file.

All files in the zipped archive will be unzipped to actual directory & subdirectory structure in one go.

Before using this code, install the python library ‘zipfile36’ or ‘zipfile38’ using one of these commands. These commands has to be run from your command prompt.

pip install zipfile38
pip install zipfile36

The above step will download zipfile library & install in your computer. Then use the python unzip code in next sections.

Python Unzip Files

Just like adding files to a Zip archvie this Python code also uses the ‘Zipfile’ Python open source library.

#Import Libraries
import zipfile

#Directory path to extract files
dirpath = 'E:\ArchiveUnzip'

#Create Zipfile object to process the Archive file.
with zipfile.ZipFile('E:\Archive.zip', 'r') as unzipObject:

    #Extract all files in one Go
    unzipObject.extractall(dirpath)

print ("All files in Zip archive are extracted")

Replace the directory path & Zip file path in the above code.

Then run the code & verify the destination path. All the files in the archive will be extracted to the given destination directory and subdirectory paths.

External Reference:

  • Discussion on how to unzip files from a directory – Click here.
  • Learn how to Zip a file in Python – Click here.