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
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
+ 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')
+ 3
with ZipFile('spam.zip') as myzip:
with myzip.open('eggs.txt', 'r', 'password') as myfile:
print(myfile.read())
+ 2
with ZipFile('filename.zip') as zip_file:
zip_file.open('filename.txt', 'r', 'password')
+ 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.)
+ 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())
+ 1
with ZipFile('spam.zip') as myzip:
with myzip.open('eggs.txt') as myfile:
print(myfile.read())
+ 1
And I found another solution from stackoverflow
'password'.encode('cp850','replace')
0
Thanks Gordon, but I've already tried without the 'pwd=' and is the same
0
Same problems with the password
0
It doesn't work 😔
0
Try following exactly the same as Python doc
with as then with as
This time should work 😉
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?
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
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'