{"id":5806,"date":"2021-05-13T12:18:13","date_gmt":"2021-05-13T06:48:13","guid":{"rendered":"https:\/\/python-programs.com\/?p=5806"},"modified":"2021-11-22T18:42:44","modified_gmt":"2021-11-22T13:12:44","slug":"python-how-to-unzip-a-file-extract-single-multiple-or-all-files-from-a-zip-archive","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-unzip-a-file-extract-single-multiple-or-all-files-from-a-zip-archive\/","title":{"rendered":"Python: How to unzip a file | Extract Single, multiple or all files from a ZIP archive | Syntax for ZipFile"},"content":{"rendered":"
In this tutorial, we will discuss different ways to unzip or extract single, multiple, or all files from the zip archive to a current or different directory. Also, you can learn what is a zip file and python unzip file along with the syntax. Moreover, let’s check the reasons to use the Zip Files in python. Click on the direct links available below and happily understand what you required regarding How to unzip files in Python?<\/strong><\/p>\n A ZIP file is a single file containing one or more compressed files, it is an easy way to make large files smaller and keep related files together.<\/p>\n ZipFile class provides a member function to extract all the data from a ZIP archive. ZIP is the archive file format that supports lossless data compression.<\/p>\n In order to unzip a file in Python, make use of this ZipFile.extractall() method. The extractall() method takes a path, members, pwd\u00a0<\/strong>as an argument, and extracts all the contents.<\/p>\n Also Check:<\/span><\/p>\n It accepts three arguments-<\/p>\n 1. path: It shows the location where we want to extract our file<\/p>\n 2. members:\u00a0 It shows the list of files to be extracted<\/p>\n 3. pwd: If the zip file is encrypted then pass the password in this argument default it will be None<\/p>\n Let\u2019s assume, in my current working directory, I have a zip file called Mail3.zip, and I wanted to unzip it with the help of the ZipFile.extractall() method. So, check out the following example program code:<\/p>\n Output:<\/p>\n Lets’ first, import the zipfile module in our program,<\/p>\n So if want to zip file \u2018program.zip\u2019. in our current directory, let\u2019s see how to extract all files from it. In the output, it will extract the files in the same directory as your programming app.py file.<\/p>\n To extract all the files from a zip file to another directory we can pass the destination location as an argument in extractall().<\/p>\n\n
<\/a>What is Zip File?<\/h2>\n
<\/a>Why do we need zip files?<\/h3>\n
\n
<\/a>Python Unzip File<\/h2>\n
\n
<\/a>Syntax for ZipFile<\/h3>\n
ZipFile.extractall(path=None, members=None, pwd=None)<\/code><\/p>\n
<\/a>Example of Python unzip<\/h3>\n
# app.py\r\n\r\nfrom zipfile import ZipFile\r\n\r\nwith ZipFile('Mail3.zip', 'r') as zipObj:\r\n # Extract all the contents of zip file in current directory\r\n zipObj.extractall()<\/pre>\n
python3 app.py<\/pre>\n
from zipfile import ZipFile<\/code><\/p>\n
<\/a>Extract all files from a zip file to the current directory<\/h2>\n
\nTo unzip it first create a ZipFile object by opening the zip file in read mode and then call extractall() on that object.<\/p>\n# app.py\r\n\r\nfrom zipfile import ZipFile\r\n\r\nwith ZipFile('program.zip', 'r') as zipObj:\r\n # Extract all the contents of zip file in current directory\r\n zipObj.extractall()<\/pre>\n
<\/a>Extract all files from a zip file to Different Directory<\/h2>\n
#\u00a0app.py\u00a0\r\nfrom\u00a0zipfile\u00a0import\u00a0ZipFile\r\n# Create a ZipFile Object and load program.zip in it\r\nwith ZipFile('program.zip', 'r') as zipObj:\r\n # Extract all the contents of zip file in different directory\r\n zipObj.extractall('temp')\r\n<\/pre>\n