Python: How to unzip a file | Extract Single, multiple or all files from a ZIP archive

In this post i will learn to different methods to python unzip or extract list of pairs single file on root python unzip all files in directory, multiple files or all files from zip archive to main current or different some folder using python unzip iterator.

python if in list,list contains python,python array contains,python list contains string,check if element in list python,iterate through list python,python find index of item in list,python check for duplicates in list,check if something is in a list python,how to check if something is in a list python,loop through list python
python if in list,list contains python,python array contains,python list contains string,check if element in list python,iterate through list python,python find index of item in list,python check for duplicates in list,check if something is in a list python,how to check if something is in a list python,loop through list python

In Python’s main zipfile module using python unzip zip object, ZipFile class supports a member function to extract all the data contents from a ZIP archive with python unzip file in memory,

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

It accepts bellow some arguments : like as a “python unzip file with password”.

Free Live Chat for Any Issue
  • path : main root location path where zip file required to be all data extracted files and folder, if not supported it will extract the data contents in ongoing directory.
  • members : here some example of member list of files to be extracted as well as python unzip list of tuples. It will extract all the data files in zip if this parameters is not supported.
  • pwd : here pwd is a main password If zip file is encrypted then pass password in this parameters default standard is None.

Also Read: how to skip a line in python | python skip lines starting with #

Module required : python unzip gz file

from zipfile import ZipFile

Let’s use this to python extract all the data contents from zip files.

Also Read: Shell Script to Add Two Numbers using ommand line arguments

Extract Single, multiple or all files from a ZIP archive

Suppose i have a zip file ‘pakainfo.zip’. in our ongoing directory, let’s display how to extract all files from it.
To unzip it first make a ZipFile object by opening the zip file in only permittions read mode and then call simple extractall() on that object bellow Example Like as a

# make a ZipFile Object and load pakainfo.zip in it
with ZipFile('pakainfoDir.zip', 'r') as zipObj:
   # Extract all the contents of zip file in ongoing directory
   zipObj.extractall()

It will extract all the files in zip at ongoing Directory. If lots of the files with same name are already present at extraction main location then it will overwrite those files.

Also Read This 👉   remove all occurrences of a character in a list python

Also Read: how to split a string in python? | python split string into list

Python Unzip: How To Extract Single or Multiple Files

To all the root file data extract all the files using zipObj from zip file to a various folder, i can pass the destination location as parameters in extractall(). here learn to main Path can be relative get all the files or absolute path.

# make a ZipFile Object and load pakainfo.zip in it
with ZipFile('pakainfoDir.zip', 'r') as zipObj:
   # Extract all the contents of zip file in various  directory
   zipObj.extractall('temp')

It will extract all the files in ‘pakainfo.zip’ in temp folder.

Also Read: python list contains

unzip — list, test and extract compressed files in a ZIP archive

The file name, a period (or “dot”), and the file name extension. Suppose i have a very large of the files and folder zip file as well as i required a few files from lots of data files in the archive. here also i learn to Unzipping all files from large means lots of the data files and folder zip can take minutes(Quickly). So if are interested in more of the zip archived files single folder only, then simply Last step to instead of unzipping the whole file and directory i can using commmands extract a single file too from the main zip file.

Also Read This 👉   Linear Search in C, C++, JAVA, PHP, Python3 and C#

In Python’s zipfile main module, useful like as a ZipFile class supports a user define a member function to all the files extract a single from a ZIP File and folder like bellow example.,

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

Also Read: Linear Search in C, C++, JAVA, PHP, Python3 and C#

It accepts following arguments :

  • member : Full name of file to be extracted. here define It should one from the all the member list of archived files as well as folder names returned by simple object of the python ZipFile.namelist()
  • path : location where zip file need to be extracted, if not provided it will extract the file in ongoing directory.
  • pwd : here pwd is a main password If zip file is encrypted then pass password in this parameters default standard is None.

Also Read: convert string to array python

Let’s use this to data extract only csv files from a zip file Example

# make a ZipFile Object as well as load pakainfo.zip in it
with ZipFile('pakainfoDir.zip', 'r') as zipObj:
   # fetch a list of all the main folder archived file names from the zip
   listOfFileNames = zipObj.namelist()
   # Iterate over the file names
   for fileName in listOfFileNames:
       # Check filename endswith csv
       if fileName.endswith('.csv'):
           # zipObj Extract a single file from zip
           zipObj.extract(fileName, 'temp_csv')

Also Read: How to Use Time Sleep in Python Script?

here fully complete Example extract only main csv data files from given zip archive using Python.

Complete example is as follows,

from zipfile import ZipFile
def main():
    print('Extract all files in ZIP to ongoing directory')
    # make a ZipFile Object as well as load pakainfo.zip in it
    with ZipFile('pakainfoDir.zip', 'r') as zipObj:
       # zipObj Extract all the data contents of zip file in ongoing directory
       zipObj.extractall()
    print('Extract all files in ZIP to different directory')
    # make a ZipFile Object as well as load pakainfo.zip in it
    with ZipFile('pakainfoDir.zip', 'r') as zipObj:
       # zipObj Extract all the data contents of zip file in different directory
       zipObj.extractall('temp')
    print('zipObj Extract single file from ZIP')
    # make a ZipFile Object as well as load pakainfo.zip in it
    with ZipFile('pakainfoDir.zip', 'r') as zipObj:
       # fetch a list of all archived file names from the zip
       listOfFileNames = zipObj.namelist()
       # Iterate over the file names
       for fileName in listOfFileNames:
           # Check filename endswith csv
           if fileName.endswith('.csv'):
               # zipObj Extract a single file from zip
               zipObj.extract(fileName, 'temp_csv')
if __name__ == '__main__':
   main()

Also Read: python developer resume Examples

Rate this post