Tap*_*eak 30 python zip zipfile python-2.4
我很难弄清楚如何用2.4解压缩zip文件.extract()不包括在2.4中.我只能在我的服务器上使用2.4.4.
有人可以提供一个简单的代码示例吗?
Vin*_*vic 51
你必须使用namelist()和extract().考虑目录的示例
import zipfile
import os.path
import os
zfile = zipfile.ZipFile("test.zip")
for name in zfile.namelist():
(dirname, filename) = os.path.split(name)
print "Decompressing " + filename + " on " + dirname
if not os.path.exists(dirname):
os.makedirs(dirname)
zfile.extract(name, dirname)
Run Code Online (Sandbox Code Playgroud)
Ovi*_*lia 12
Vinko的答案存在一些问题(至少在我运行时).我有:
IOError: [Errno 13] Permission denied: '01org-webapps-countingbeads-422c4e1/'
Run Code Online (Sandbox Code Playgroud)
以下是解决方法:
# unzip a file
def unzip(path):
zfile = zipfile.ZipFile(path)
for name in zfile.namelist():
(dirname, filename) = os.path.split(name)
if filename == '':
# directory
if not os.path.exists(dirname):
os.mkdir(dirname)
else:
# file
fd = open(name, 'w')
fd.write(zfile.read(name))
fd.close()
zfile.close()
Run Code Online (Sandbox Code Playgroud)