简体   繁体   English

Python zipfile在Windows上部分起作用吗?

[英]Python zipfile works partially on Windows?

I'm using zipfile in my script to unzip .zip files. 我在脚本中使用zipfile解压缩.zip文件。 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. 当我在Linux或OS X上解压缩文件时,它可以正常工作,但是当我在Windows上运行该文件时,它会创建一个目录以及其中的所有目录,但没有一个文件。 Why might this be? 为什么会这样呢?

For compatibility with Unix-like and Windows, replace 为了与类Unix和Windows兼容,请更换

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. 在类Unix上将使用/ ,在Windows上使用\\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM