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

ぃ、小莉子 提交于 2019-12-04 19:13:14

问题


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.


回答1:


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)



回答2:


unzip, defined below, is what you want.

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


来源:https://stackoverflow.com/questions/19483775/python-zipfile-extract-doesnt-extract-all-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!