Python Language

Unzipping Files

Introduction#

To extract or uncompress a tarball, ZIP, or gzip file, Python’s tarfile, zipfile, and gzip modules are provided respectively. Python’s tarfile module provides the TarFile.extractall(path=".", members=None) function for extracting from a tarball file. Python’s zipfile module provides the ZipFile.extractall([path[, members[, pwd]]]) function for extracting or unzipping ZIP compressed files. Finally, Python’s gzip module provides the GzipFile class for decompressing.

Using Python ZipFile.extractall() to decompress a ZIP file

file_unzip = 'filename.zip'
unzip = zipfile.ZipFile(file_unzip, 'r')
unzip.extractall()
unzip.close()

Using Python TarFile.extractall() to decompress a tarball

file_untar = 'filename.tar.gz'
untar = tarfile.TarFile(file_untar)
untar.extractall()
untar.close()

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow