Python zipfile.extract()不提取所有文件

Dan*_*vic 11 python zip extract

我正在尝试使用此处的代码提取压缩文件夹.

def unzip(source_filename, dest_dir):
with zipfile.ZipFile(source_filename) as zf:

    for member in zf.infolist():
        words = member.filename.split('/')
        path = dest_dir
        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)
Run Code Online (Sandbox Code Playgroud)

但是当试图提取,例如,wordpress.zip与目录结构
wordpress/
-wp-content/
--- somefile.php
-wp-config.php
-index.php
我只获取根文件夹或wordpress下面的文件夹中的文件/ 在这种情况下.所以我得到wordpress/wp-content/somefile.php但不是wordpress /文件夹本身的文件.

Rob*_*obᵩ 21

首先要看的是文档:

ZipFile.extractall([path[, members[, pwd]]])
Run Code Online (Sandbox Code Playgroud)

应用于您的情况,我会尝试:

def unzip(source_filename, dest_dir):
    with zipfile.ZipFile(source_filename) as zf:
        zf.extractall(dest_dir)
Run Code Online (Sandbox Code Playgroud)