如何在 Python 中为压缩的 zip 文件设置密码
当我尝试为 zip 文件设置密码时出现此错误。下面是我得到的代码/错误。请给我一个正确方法的例子。
- 这只是脚本的密码部分...整个脚本太长了,无法发布。
代码:
password = "dog"
password = zipfile.setpassword(pwd)
Error received when hitting the password part of the script.
-------------------------------------------
Traceback (most recent call last):
File "C:\Users\Owner\Desktop\ZIP-IT\ZIP IT.py", line 86, in <module>
start()
File "C:\Users\Owner\Desktop\ZIP-IT\ZIP IT.py", line 54, in start
compress()
File "C:\Users\Owner\Desktop\ZIP-IT\ZIP IT.py", line 70, in compress
password = zipfile.setpassword(pwd)
AttributeError: 'module' object has no attribute 'setpassword'
I get this error when I try to set a password to a zip file. Below is the code/error I get. Please give me an example of the correct way to do it.
- This is just the password part of the script... the entire script is to long to post.
Code:
password = "dog"
password = zipfile.setpassword(pwd)
Error received when hitting the password part of the script.
-------------------------------------------
Traceback (most recent call last):
File "C:\Users\Owner\Desktop\ZIP-IT\ZIP IT.py", line 86, in <module>
start()
File "C:\Users\Owner\Desktop\ZIP-IT\ZIP IT.py", line 54, in start
compress()
File "C:\Users\Owner\Desktop\ZIP-IT\ZIP IT.py", line 70, in compress
password = zipfile.setpassword(pwd)
AttributeError: 'module' object has no attribute 'setpassword'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您运行的是 Python 2.6+ 吗?
ZipFile.setpassword(pwd)
将 pwd 设置为默认密码提取加密文件。
版本 2.6 中的新增功能。
Python zipfile 文档 位于顶部表示,他们“[支持]解密 ZIP 存档中的加密文件,但目前无法创建加密文件。”
Are you running Python 2.6+?
ZipFile.setpassword(pwd)
Set pwd as default password to extract encrypted files.
New in version 2.6.
The Python zipfile docs say at the top that they "[support] decryption of encrypted files in ZIP archives, but it currently cannot create an encrypted file."
您需要引用特定的 zip,而不是模块。
You need to reference the particular zip, instead of the module.