从 zip 中提取某些文件但不完整目录

kis*_*ore 3 python zip directory-structure shutil

我已经实现了以下代码,将特定文件从 zip 复制到某个目标目录。

但这会将整个结构复制到目标目录中。代码是:

import os
import zipfile 

zip_filepath='/home/sundeep/Desktop/SCHEMA AUTOMATION/SOURCE/DSP8010_2017.1.zip'
target_dir='/home/sundeep/Desktop/SCHEMA AUTOMATION/SCHEMA'

with zipfile.ZipFile(zip_filepath) as zf:
    dirname = target_dir
    zf.extract('DSP8010_2017.1/json-schema/AccountService.json',path=dirname)
Run Code Online (Sandbox Code Playgroud)

我的问题是如何仅将 AccountService.json 文件复制到目标目录而不是整个结构。实施shutil有可能吗?

kis*_*ore 5

import os
import shutil
import zipfile

zip_filepath='/home/sundeep/Desktop/SCHEMA AUTOMATION/SOURCE/DSP8010_2017.1.zip'
target_dir='/home/sundeep/Desktop/SCHEMA AUTOMATION/SCHEMA'


with zipfile.ZipFile(zip_filepath) as z:
    with z.open('DSP8010_2017.1/json-schema/AccountService.json') as zf, open(os.path.join(target_dir, os.path.basename('AccountService.json')), 'wb') as f:
        shutil.copyfileobj(zf, f) 
Run Code Online (Sandbox Code Playgroud)