HomeLinuxThe best way to Unzip Recordsdata in Python

The best way to Unzip Recordsdata in Python


Compressed information take up much less house, which makes them simpler to switch over the web or retailer in your onerous drive. Nevertheless, they must be unzipped or extracted earlier than they are often utilized. As an example, when you’ve got a big dataset in a zipped file format, you must extract it to a folder with the intention to entry its contents.

On this article, we are going to discover totally different approaches to unzipping information in Python.

The best way to Unzip Recordsdata in Python?

To unzip information in Python, apply the next approaches:

Methodology 1: Unzip Recordsdata in Python Utilizing “zipfile” Module

The “zip” file may be extracted utilizing the “zipfile” module in Python. This module makes use of the “extractall()” methodology to extract all of the information from a zipper file to a specified listing. The “extract()” methodology, nonetheless, extracts a single file or a listing of information from a zipper file to a specified listing.

Syntax

ZipFile.extractall(path=None, members=None, pwd=None)

 

ZipFile.extract(member, path=None, pwd=None)

 

The unique path and the title of the “zip” file that must be extracted all through the weblog are proven within the beneath snippet:

Instance 1: Extract All of the Recordsdata From a Zip File Utilizing the “zipfile extractall()” Methodology

Let’s overview the next instance code:

import zipfile
with zipfile.ZipFile(r“C:UserspDocumentsprogramsample.zip”, “r”) as zip:
    zip.extractall(r“C:UserspDocumentsprogram”)

 

Within the above code, the “zipfile.ZipFile()” perform is used to open the zip file, and the “zip.extractall()” methodology extracts all of the information from the desired zip file.

Output

The above end result implies that every one the information from a zipper file have been extracted.

Instance 2: Extract a Single File From a Zip File Through the “zipfile extract()” Methodology

The beneath code is used to extract a single file from the zip file:

import zipfile
with zipfile.ZipFile(r“C:UserspDocumentsprogramsample.zip”, “r”) as zip:
    zip.extract(“instance.py”, r“C:UserspDocumentsprogram”)

 

Within the above code, the “zipfile.ZipFile()” perform is used to open the zip file, and the “zip.extract()” methodology is utilized to extract the particular file from the given zip file.

Output

Primarily based on the above output, the one file from the zip file is extracted.

Methodology 2: Unzip Recordsdata in Python Utilizing the “shutil” Module

One other strategy to unzip information in Python is through the “shutil” module. This module offers a higher-level interface for working with information and directories. The “unpack_archive()” methodology of the “shutil” module is used to extract an archive file to a specified listing. This methodology helps numerous archive codecs, resembling “zip”, “tar”, “gz”, and so on.

Syntax

shutil.unpack_archive(filename, extract_dir[, format])

 

Instance

Undergo the below-stated code snippet:

import shutil
shutil.unpack_archive(r“C:UserspDocumentsprogramsample.zip”, r“C:UserspDocumentsprogram”, ‘zip’)

 

In accordance with the above traces of code, the “shutil.unpack_archive()” methodology takes the zip file’s full path and the goal listing as its arguments, respectively, and unzips the information accordingly.

Output

Primarily based on the above output, each information have been extracted to the desired path appropriately.

Conclusion

The “zipfile” module and the “shutil” module are used to unzip single or a number of information from the desired zip file in Python. The “extractall()” methodology and “extract()” methodology of the “zipfile” module are used to unzip the one or all of the information of the given zip file, respectively. The “unpack_archive()” methodology of the “shutil” module is used to extract an archive file to a specified listing. This submit offered numerous methods to unzip the desired zip file utilizing quite a few examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments