为什么python不能使用zip方法解压缩由winrar创建的受密码保护的zip文件? [英] why can't python unzip a password protected zip file created by winrar using the zip method?

查看:125
本文介绍了为什么python不能使用zip方法解压缩由winrar创建的受密码保护的zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上搜索了很多,但仍然没有找到解决上述问题的方法.有没有人知道为什么,如果可以,怎么做?

I have searched the web high and low but still couldn't find a solution for the above problem. Does anyone out there know why and if so how it can be done?

psw="dg"

ZipFile.extractall("data.zip", None, psw)

我遇到的错误:

TypeError: unbound method extractall() must be called
with ZipFile instance as first argument (got str instance instead)

推荐答案

因为你用错了.:) 来自 docs:

Because you are using it wrong. :) From docs:

ZipFile.extractall([path[, members[, pwd]]])

ZipFile.extractall([path[, members[, pwd]]])

从存档中提取所有成员到当前工作目录.path 指定要提取到的不同目录.members 是可选的,并且必须是 namelist() 返回的列表的子集.密码是用于加密文件的密码.

Extract all members from the archive to the current working directory. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by namelist(). pwd is the password used for encrypted files.

所以你应该为 ZipFile 对象调用这个函数,而不是作为静态方法.并且您不应将存档名称作为第一个参数传递.:)

So you should call that this function for ZipFile object, not as static method. And you should not pass name of archive as a first argument. :)

这样就可以了:

from zipfile import ZipFile

with ZipFile('data.zip') as zf:
    zf.extractall(pwd='dg'

EDIT,在较新的版本中使用:

EDIT, in newer versions use:

zf.extractall(pwd=b'dg')

这篇关于为什么python不能使用zip方法解压缩由winrar创建的受密码保护的zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆