Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从zip文件内的目录中提取_Python_Python 3.x_Zip_Zipfile - Fatal编程技术网

Python 从zip文件内的目录中提取

Python 从zip文件内的目录中提取,python,python-3.x,zip,zipfile,Python,Python 3.x,Zip,Zipfile,我现有的代码(一个部分)放在这篇文章的底部 我想做的是从zip文件内的目录中提取所有文件。不是zip文件的全部内容,只是zip中目录(/theme\u files/)中的文件。我已经导入了必要的库 将文件从[filename].tpk/theme_文件提取到/workspace/[output]/ (.tpk=.zip) 使用regex+搜索特定子文件夹中的文件名逐个文件提取它们: with open('<file-name>.zip', 'rb') as f: zf = z

我现有的代码(一个部分)放在这篇文章的底部

我想做的是从zip文件内的目录中提取所有文件。不是zip文件的全部内容,只是zip中目录(
/theme\u files/
)中的文件。我已经导入了必要的库

将文件从
[filename].tpk/theme_文件
提取到
/workspace/[output]/

.tpk
=
.zip


使用regex+搜索特定子文件夹中的文件名逐个文件提取它们:

with open('<file-name>.zip', 'rb') as f:
    zf = zipfile.ZipFile(f)
    for file in [m.group() for m in (re.search(r'/theme/(.+)', file) for file in file_list) if m]:
        zf.extract(file)
以open('.zip',rb')作为f的
:
zf=zipfile.zipfile(f)
对于[m.group()中的文件,对于m in(如果m,则重新搜索(r'/theme/(.+)',file)中的文件,对于文件列表中的文件):
zf.extract(文件)
您可以使用对从以下位置返回的文件名的列表理解,使用并设置
路径
成员
参数:


这仅适用于将
主题_文件
文件夹复制到输出中。知道如何将
主题文件的内容提取到输出中吗?它应该提取文件夹及其包含的所有内容。你没有这样吗?没有。它提取整个文件夹并将其放入
output\u dir
for item in (f for f in theme_zip.filelist if 'theme_files/' in f.filename):
    theme_zip.extract(item, output_dir)
with open('<file-name>.zip', 'rb') as f:
    zf = zipfile.ZipFile(f)
    for file in [m.group() for m in (re.search(r'/theme/(.+)', file) for file in file_list) if m]:
        zf.extract(file)
import zipfile 

output_dir = './workspace/[output]/'
file = current_dir + "/themes/" + theme_name + ".tpk"

with zipfile.ZipFile(file, 'r') as f:
    files = [n for n in f.namelist() 
                   if n.startswith('/theme_files/') and not n.endswith('/')]
    f.extractall(path=output_dir , members=files)