extract zip files using python

12,044

Python zipfile package can unzip files having password.

def unzip_folder(zip_folder, destination, pwd):
        """
        Args:
            zip_folder (string): zip folder to be unzipped
            destination (string): path of destination folder
            pwd(string): zip folder password

        """
        with zipfile.ZipFile(zip_folder) as zf:
            zf.extractall(
                destination, pwd=pwd.encode())

In your case,

import zipfile
zip_folder = 'E:\\Shared\\DOWNLOADED\\c.zip'
destination = 'E:\\Shared\\DOWNLOADED'
pwd = '<YOUR_PASSWORD>'

with zipfile.ZipFile(zip_folder) as zf:
    zf.extractall(
        destination, pwd=pwd.encode())
Share:
12,044
C.E James
Author by

C.E James

Updated on June 11, 2022

Comments

  • C.E James
    C.E James over 1 year
    import zipfile
    
    fantasy_zip = zipfile.ZipFile('E:\\Shared\\DOWNLOADED\\c.zip')
    fantasy_zip.extractall('E:\\Shared\\DOWNLOADED\\extract)
    
    fantasy_zip.close()
    

    my password is "hello" how can I include password to extract?

  • C.E James
    C.E James over 5 years
    Thank you for the help, based on docs.python.org/2/library/zipfile.html the codes works when I am using a zip file without password.
  • C.E James
    C.E James over 5 years
    Thank you for your help, based on the error I got, invalid syntax in line number 1
  • Sijan Bhandari
    Sijan Bhandari over 5 years
    I don't get it.Line number. 1 in your syntax or in mine?
  • C.E James
    C.E James over 5 years
    def unzip_folder("E:\\Shared\\DOWNLOADED\\c.zip", "E:\\Shared\\DOWNLOADED\\extract", "virus"): """ Args: zip_folder (string): zip folder to be unzipped destination (string): path of destination folder pwd(string): zip folder password """ with zipfile.ZipFile(zip_folder) as zf: zf.extractall( destination, pwd=pwd.encode())
  • C.E James
    C.E James over 5 years
    this is the error: File "E:\Shared\DOWNLOADED\sample.py", line 1 def unzip_folder("E:\\Shared\\DOWNLOADED\\c.zip", "E:\\Shared\\DOWNLOADED\\extract", "virus"): ^ SyntaxError: invalid syntax
  • Sijan Bhandari
    Sijan Bhandari over 5 years
    Why there are values in quote (" ") in your function definition?
  • C.E James
    C.E James over 5 years
    should I remove all the double quotes?
  • Sijan Bhandari
    Sijan Bhandari over 5 years
    I have edited my answer above. For your case, you can simply define variables for those arguments and call unzip.
  • C.E James
    C.E James over 5 years
    File "C:\Python27\lib\zipfile.py", line 1040, in extractall self.extract(zipinfo, path, pwd) File "C:\Python27\lib\zipfile.py", line 1028, in extract return self._extract_member(member, path, pwd) File "C:\Python27\lib\zipfile.py", line 1082, in _extract_member with self.open(member, pwd=pwd) as source, \ File "C:\Python27\lib\zipfile.py", line 1007, in open raise RuntimeError("Bad password for file", name) RuntimeError: ('Bad password for file', <zipfile.ZipInfo object at 0x03277850>)
  • Sijan Bhandari
    Sijan Bhandari over 5 years
    You have set wrong password. Do you know the password of the zipfile? It is clear in your error message.
  • Sijan Bhandari
    Sijan Bhandari over 5 years
    I am glad it helps you out.