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
with
This will use a
/on Unix-like, and a\on Windows.