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())
Author by
C.E James
Updated on June 11, 2022Comments
-
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 over 5 yearsThank 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 over 5 yearsThank you for your help, based on the error I got, invalid syntax in line number 1
-
Sijan Bhandari over 5 yearsI don't get it.Line number. 1 in your syntax or in mine?
-
C.E James over 5 yearsdef 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 over 5 yearsthis 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 over 5 yearsWhy there are values in quote (" ") in your function definition?
-
C.E James over 5 yearsshould I remove all the double quotes?
-
Sijan Bhandari over 5 yearsI have edited my answer above. For your case, you can simply define variables for those arguments and call unzip.
-
C.E James over 5 yearsFile "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 over 5 yearsYou have set wrong password. Do you know the password of the zipfile? It is clear in your error message.
-
Sijan Bhandari over 5 yearsI am glad it helps you out.