Python provides several ways to unzip files. One way is to use the built-in zipfile module. This module provides tools to create, read, write, append, and list ZIP files. It also includes a corresponding ZipExtFile class that can be used to read or write compressed data within ZIP files.
To unzip a file, you can use the ZipFile method. This method takes two arguments: the name of the zip file and the mode.
The mode argument must be “r” to read files from the zip file or “w” to write files to the zip file. For example, to unzip a file named “ex.zip”, you would use the following code:
import zipfile zipf = zipfile.ZipFile("ex.zip", "r") zipf.extractall()
This code would extract all of the files from the zip file and save them in the current directory.
Or you can enter a file to extract by this code
import zipfile target = input(r"Enter file to be unzipped: ") handle = zipfile.ZipFile(target) handle.extractall("./Unzip file/Unzip files") handle.close()
in Python’s zipfile module, ZipFile class provides a member function to extract all the contents from a ZIP archive,
ZipFile.extractall(path=None, members=None, pwd=None)
It accepts the following arguments :
- path: location where zip file needs to be extracted, if not provided it will extract the contents in the current directory.
- members: list of files to be extracted. It will extract all the files in zip if this argument is not provided.
- pwd: If the zip file is encrypted then the password in this argument default is None.
Unzip File Instructions: 👨🏻💻
Open Termnial 💻
Locate to the directory where python file is located 📂
Run the command: python script.py/python3 script.py 🧐