Python zipfile.extract()不会提取所有文件 [英] Python zipfile.extract() doesn't extract all files

查看:603
本文介绍了Python zipfile.extract()不会提取所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

但是在尝试提取具有目录结构的wordpress.zip时,
wordpress/
-wp-content/
--- somefile.php
-wp-config.php
-index.php
在这种情况下,我只会在根文件夹或wordpress/下的文件夹中获取文件.所以我得到了wordpress/wp-content/somefile.php,但没有wordpress/文件夹中的文件.

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.

推荐答案

第一个要看的地方是文档:

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)

这篇关于Python zipfile.extract()不会提取所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆