Problem with zip file with password | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Problem with zip file with password

Hi, I'm trying to unzip a file, open it in read mode and print the content but I'm having problems with the password. In the code below there are the descriptions and the errors. I'm using python 3.7 on my computer. Thank you for the help https://code.sololearn.com/cx46S2442zJX/?ref=app https://code.sololearn.com/cx46S2442zJX/?ref=app

21st Jan 2019, 11:26 AM
Sander
15 Answers
+ 5
zip_file.open('filename.txt', 'r', 'password') pwd is the parameter name of the open() method, when you use the method you usually don't need to put the parameter name in passing argument unless you are using dictionary to pass the argument as a keyworded argument https://docs.python.org/3.7/library/zipfile.html#zipfile.ZipFile.open
21st Jan 2019, 11:50 AM
Gordon
Gordon - avatar
+ 3
Replace zip_file = ZipFile('filename.zip') with with ZipFile('filename.zip') as zip_file: And then indent zip_file.open('filename.txt', 'r', pwd='password') In short, the code should be with ZipFile('filename.zip') as zip_file: zip_file.open('filename.txt', 'r', pwd='password')
21st Jan 2019, 12:22 PM
Gordon
Gordon - avatar
+ 3
with ZipFile('spam.zip') as myzip: with myzip.open('eggs.txt', 'r', 'password') as myfile: print(myfile.read())
21st Jan 2019, 3:06 PM
Gordon
Gordon - avatar
+ 2
with ZipFile('filename.zip') as zip_file: zip_file.open('filename.txt', 'r', 'password')
21st Jan 2019, 12:38 PM
Gordon
Gordon - avatar
+ 2
If the last one still didn't work, can you copy the full error message to here? (Bed time here, will come back to this again tomorrow.)
21st Jan 2019, 3:47 PM
Gordon
Gordon - avatar
+ 2
I used Python console to try actually unzip a file once, and we are just one step close: Add a b before 'password' making it b'password' The final code which works: from zipfile import * with ZipFile('filename.zip') as myzip: with myzip.open('filename.txt', 'r', b'password') as myfile: print(myfile.read())
22nd Jan 2019, 4:45 AM
Gordon
Gordon - avatar
+ 1
with ZipFile('spam.zip') as myzip: with myzip.open('eggs.txt') as myfile: print(myfile.read())
21st Jan 2019, 12:41 PM
Gordon
Gordon - avatar
+ 1
And I found another solution from stackoverflow 'password'.encode('cp850','replace')
22nd Jan 2019, 4:49 AM
Gordon
Gordon - avatar
0
Thanks Gordon, but I've already tried without the 'pwd=' and is the same
21st Jan 2019, 12:14 PM
Sander
0
Same problems with the password
21st Jan 2019, 12:37 PM
Sander
0
It doesn't work 😔
21st Jan 2019, 12:40 PM
Sander
0
Try following exactly the same as Python doc with as then with as This time should work 😉
21st Jan 2019, 12:42 PM
Gordon
Gordon - avatar
0
I'm sorry @Gordon but it doesn't work. If I use open without the password I have this error: RuntimeError: File 'filename.txt' is encrypted, password required for extraction, and if i use open with the password I have the same problems. Maybe I need to use some encode methods?
21st Jan 2019, 12:59 PM
Sander
0
I tried but same error. - The code: from zipfile import * with ZipFile('filename.zip') as myzip: with myzip.open('filename.txt', 'r', 'password') as myfile: print(myfile.read()) - The error: Traceback (most recent call last): File "C:\Users...", line 3, in <module> with myzip.open('filename.txt', 'r', 'password') as myfile: File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1428, in open raise TypeError("pwd: expected bytes, got %s" % type(pwd).__name__) TypeError: pwd: expected bytes, got str Thanks @Gordon, see you tomorrow
21st Jan 2019, 4:17 PM
Sander
0
Sorry @Gordon but I been busy. I tried your last solution but I'm sorry to disappoint you it doesn't work. Code: from zipfile import * with ZipFile('filename.zip') as myzip: with myzip.open('filename.txt', 'r', 'password'.encode('cp850', 'replace')) as myfile: print(myfile.read()) Error: Traceback (most recent call last): File "C:\Users\...", line 3, in <module> with myzip.open('filename.txt', 'r', 'password'.encode('cp850', 'replace')) as myfile: File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1516, in open raise RuntimeError("Bad password for file %r" % name) RuntimeError: Bad password for file 'filename.txt'
23rd Jan 2019, 9:12 AM
Sander