To unzip a file in Python, you can use the “ZipFile.extractall()” method. The extractall() method takes a path, members, and pwd as an argument and extracts all the contents.
ZipFile.extractall(path=None, members=None, pwd=None)
It accepts the following parameters :
- path: location where the zip file needs to be extracted; if not provided, it will extract the contents in the current directory.
- members: list of files to be removed. If this argument is not provided, it will extract all the files in the zip.
- pwd: If the zip file is encrypted, pass the password in this argument default is None.
Example of unzip() function
In my current working directory, I have a zip file called Mail3.zip, and I want to unzip it using ZipFile.extractall() method.
from zipfile import ZipFile with ZipFile('Mail3.zip', 'r') as zipObj: # Extract all the contents of zip file in current directory zipObj.extractall()
If you run the above code, it will extract the files in your programming app.py file in the same directory.
It will extract all the files in the zip at the current Directory. If files with the same name are already present at the extraction location, they will overwrite.
We used with statement to open the files. The “with” statement ensures that open file descriptors are closed automatically after program execution leaves the context of the with statement.
Extracting all files from a zip file to a different directory
We can extract all the files from the zip file to a different directory; to do that, we need to pass the destination location as an argument in extractall(). The path can be relative or absolute.
from zipfile import ZipFile with ZipFile('Mail3.zip', 'r') as zipObj: # Extract all the contents of zip file in different directory zipObj.extractall('temp') print('File is unzipped in temp folder')
A ZipFile object is called a ZipFile constructor, which accepts the zip file name and mode parameters. Next, we create the ZipFile object in READ mode and name it zipObj.
In the output. If the temp folder is not there, it will create that folder in the current Directory and unzip all the content of the Mail3.zip file.
File is unzipped in temp folder
Extracting files from a large zip file based on the condition
We have a huge zip file and need a few from thousands of files in the archive. Unzipping all files from a large zip can take minutes.
But if you are interested in only a few archived files, then instead of unzipping the whole file, we can extract the single file from the zip file.
Python ZipFile class provides a member function to extract a single from a ZIP File.
ZipFile.extract(member, path=None, pwd=None)
from zipfile import ZipFile with ZipFile('Mail3.zip', 'r') as zipObject: listOfFileNames = zipObject.namelist() for fileName in listOfFileNames: if fileName.endswith('.py'): # Extract a single file from zip zipObject.extract(fileName, 'temp_py') print('All the python files are extracted')
If the filenames have the .py extension, they will extract them inside the temp_py folder. Otherwise, it won’t extract the files.
1 thought on “Python unzip: How to Extract Single or Multiple Files”
I don’t have .zip files, but ones ending with ,el; ,en; ,es; ,kl; ,no; ,ta; ,ko; and ,zh-CN; all downloaded after running PIP on a Coursera course. How do I decompress these ones?