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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19483775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:52:59  来源:igfitidea点击:

Python zipfile.extract() doesn't extract all files

pythonzipextract

提问by Danijel Cwix Predojevic

I'm trying to extract zipped folder using code found here.

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

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)

But when trying to extract, for example, wordpress.zip with directory structure
wordpress/
-wp-content/
---somefile.php
-wp-config.php
-index.php
I only get the files in folder below root folder or wordpress/ in this case. So i get wordpress/wp-content/somefile.php but not the files in the wordpress/ folder itself.

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

采纳答案by Rob?

The first place to look is the documentation:

首先要看的是文档

ZipFile.extractall([path[, members[, pwd]]])

Applying that to your situation, I'd try:

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

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

回答by Rob?

unzip, defined below, is what you want.

unzip,定义如下,就是你想要的。

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