I'm using zipfile
in my script to unzip .zip files. Here's my code:
def unzip(src, dst):
zf = zipfile.ZipFile(src)
for member in zf.infolist():
words = filter(None, member.filename.split('/'))
path = dst
for word in words[:-1]:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir): continue
path = os.path.join(path, word)
zf.extract(member, path)
dirMacosx = "%s/__MACOSX" % (dst)
if os.path.exists(dirMacosx):
shutil.rmtree(dirMacosx)
When I unzip a file on Linux or OS X, it works fine, but when I run it on Windows, it created a directory and all the directories in it, but none of the files. Why might this be?
For compatibility with Unix-like and Windows, replace
words = filter(None, member.filename.split('/'))
with
words = filter(None, member.filename.split(os.sep))
This will use a /
on Unix-like, and a \\
on Windows.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.