unzip files python code example

Example 1: how to unzip files using zipfile module python

import zipfile
with zipfile.ZipFile("file.zip","r") as zip_ref:
    zip_ref.extractall("targetdir")

Example 2: unzip in python

import zipfile
with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
    zip_ref.extractall(directory_to_extract_to)

Example 3: how to save unzipped files in python

import zipfile
def un_zipFiles(path):
    files=os.listdir(path)
    for file in files:
        if file.endswith('.zip'):
            filePath=path+'/'+file
            zip_file = zipfile.ZipFile(filePath)
            for names in zip_file.namelist():
                zip_file.extract(names,path)
            zip_file.close()

Example 4: how to extract zip file using python

ZipFile.extractall(path=None, members=None, pwd=None)

Example 5: unzip files python

from zipfile import ZipFile

with ZipFile(file_directory, 'r') as zip:
  zip.printdir()
  zip.extractall(save_directory)