如何在 Python 中为压缩的 zip 文件设置密码

发布于 2024-09-26 16:51:53 字数 683 浏览 5 评论 0原文

当我尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若沐 2024-10-03 16:51:53

您运行的是 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."

浅沫记忆 2024-10-03 16:51:53

您需要引用特定的 zip,而不是模块。

zpf = zipfile.ZipFile('your file path')
password = "dog" 
password = zpf.setpassword(pwd)

You need to reference the particular zip, instead of the module.

zpf = zipfile.ZipFile('your file path')
password = "dog" 
password = zpf.setpassword(pwd)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文