How to unzip files using python ?

Python provides a number of modules for dealing with compressed files. The zipfile module allows you to unzip files in Python

Unzip one file

To begin, import the zipfile module:

import zipfile

myZipFile = ''

Next, create an instance of the ZipFile class, which will represent our zipped file and extract the contents of myZipFile by using the extractall() method

destination_folder = '.'

with zipfile.ZipFile(myZipFile, 'r') as zip_ref:

The extractall() method takes a single argument, which is the destination path where we want our files to be extracted. The destination path must exist before calling this method, or else an error will be thrown.

Unzip multiple files

import glob

root = '/Volumes/HD2/Datasets/'

zip_files = glob.glob('{}*.zip'.format(root))


Output for example


For each zip file, iterate through it and create a separate folder to unzip its contents.

import zipfile
import os

for myzipfile in zip_files:

    destination_folder = myzipfile.split('.')[0]

    os.makedirs(destination_folder, exist_ok=True)

    with zipfile.ZipFile(myzipfile, 'r') as zip_ref:


Links Site