Unzip Files in Python
ZIP
is an archive file format used to combine multiple files with a lossless compression algorithm. ZIP
files are useful when a large number of files need to be transferred as they can be sent in one request, improving speed.
In this tutorial, we will learn how to unzip files in Python.
Unzip Files Using the zipfile Package
The Python zipfile
package provides a convenient way of extracting .zip
file archives. Once imported this package works in a similar way to the native Python open() function, except with its own functions for dealing with ZIP
files specifically.
from zipfile import ZipFile
archive = "archive.zip"
with ZipFile(archive, 'r') as zip:
zip.extractall()
The code above unzips all the files in the archive.zip
file located in the same directory as the Python program using the .extractall()
function.
Show Files in a ZIP File Without Extracting
To examine all the files inside a ZIP
archive without extracting them, use the .printdir()
from the zipfile
package like this:
from zipfile import ZipFile
archive = "archive.zip"
with ZipFile(archive, 'r') as zip:
zip.printdir()
File Name Modified Size
file.txt 2021-04-29 17:00:34 38
file2.txt 2021-04-29 17:00:34 38
file3.txt 2021-04-29 17:00:34 38
Unzip Files to a Different Directory
If you want to put the extracted files in a different directory, supply the path to the directory as the first argument of the extractall()
function like this:
from zipfile import ZipFile
archive = "archive.zip"
with ZipFile(archive, 'r') as zip:
zip.extractall('new_file')
A nice feature of the above example is if the directory supplied doesn't exist a new one will be created.
Caution – be aware that extractall()
will overwrite existing files with the same name in the output path.
Only Extract Files With a Specific Extension
In the example below, we will get a list of the files in the ZIP
archive, loop through them and only extract files with a .txt
extension.
from zipfile import ZipFile
archive = "archive.zip"
with ZipFile(archive, 'r') as zip:
files = zip.namelist()
for f in files:
if f.endswith('.txt'):
zip.extract(f)
namelist()
– get a list of the filenames in the archive.endswith()
– if the current file ends with a specified extension, extract the file from the zip object.