To unzip a file in Python, use zipfile.ZipFile to open the archive and extractall() to unzip everything to the current directory or a specified path.
Here is the archive.zip file in the temp directory:
Let’s unzip this file:
import zipfile with zipfile.ZipFile("temp/Archive.zip", "r") as zip_ref: zip_ref.extractall("temp") print("Extracting all files into a folder") # Output: Extracting all files into a folder
In the above code, we imported the built-in zipfile module and used its ZipFile() method with a context manager to open the Archive.zip file as an object.
Then, we called the .extractall() function on the file object “zip_ref” to unzip the files within it.
The .extractall() method is used for bulk extraction.
The Archive.zip file contains three files:
- app.json
- empty.txt
- output.csv
As you can see from the screenshot above, all the files are extracted into the “temp” folder.
If the ZIP contains a top-level folder, it will create that folder; otherwise, files will be extracted flat, like our example.
Extracting a single file
If you want to just extract a single file instead of a bulk extract, use the zipfile.ZipFile to open in read mode that returns a file object, and then apply the .extract() method on that object.
Here is the temp folder before extracting a single file:
Let’s extract the single file called “app.json” from it. You can extract whatever file you like.
import zipfile with zipfile.ZipFile("temp/Archive.zip", "r") as zip_ref: zip_ref.extract("app.json", "temp") print("The app.json file has been extracted") # Output: The "app.json" file has been extracted
In this code, the extract() function accepts the specific file you want to extract from the zip and the destination directory path. We passed both arguments (app.json and temp folder), and the screenshot above is the output.
Reading file content without extracting
You don’t need to extract a file to read the content. You can read the zip file object as a normal file using the with open() context manager.
import zipfile with zipfile.ZipFile("temp/Archive.zip", "r") as zip_ref: with zip_ref.open("output.csv") as f: content = f.read().decode("utf-8") print(content) # Output: # name,age,city # Marsh,30,New York # Chris,28,San Francisco # Mike,26,Los Angeles # Robert,23,Orlando # Andre,29,Illinois
Alternate approaches
Using shutil.unpack_archive
The shutil is a built-in Python module that provides the unpack_archive() method, which can be used to unzip an archive file. It supports multiple archive formats (.zip, .tar, .gztar, etc.).
Here is the temp folder screenshot before unpack:
import shutil shutil.unpack_archive("temp/Archive.zip", "temp") print("The files has been unzipped") # Output: The files has been unzipped
Here is the output screenshot:
This approach is straightforward, but it is less efficient compared to zipfile.
That’s all!
Fradrick Munyurwa
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?