3

I have implemented the following code to copy a specific file from zip to a certain target directory.

But this copies entire structure into the target directory. The code is:

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)

My question is how can I copy only AccountService.json file to target directory but not the entire structure. Any possibility by implementing shutil?

kishore
  • 127
  • 2
  • 11
  • https://stackoverflow.com/questions/17729703/extract-a-specific-file-from-a-zip-archive-without-maintaining-directory-structu – Roushan Mar 16 '18 at 05:52
  • @Roushan45 The question is quite different. There a new folder is created. But y requirement is to copying specific file to existing directory. – kishore Mar 16 '18 at 06:11

3 Answers3

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) 
kishore
  • 127
  • 2
  • 11
2

Try this:-

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:

       for file in zf.namelist():

            if file.endswith("AccountService.json"):
                zf.extract(file,target_dir)
Narendra
  • 1,511
  • 1
  • 10
  • 20
  • Its logically incorrect because the AccountService.json is present in 'DSP8010_2017.1/json-schema/AccountService.json' directory. Its not valid to use namelist operation on zf then. – kishore Mar 16 '18 at 06:40
  • Yes, it works, reduces memory consumption in case of huge files – shivaraj karki May 23 '22 at 15:41
-2

You can add file name into existing directory like this:-

a = 'DSP8010_2017.1/json-schema/AccountService.json'
dirname = target_dir+"/"+(a.split('/')[-1])

As you said having issue you can try like this:-

import zipfile

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

fantasy_zip = zipfile.ZipFile(zip_filepath)
file = fantasy_zip.extract('AccountService.json', zip_filepath)
target_dir = target_dir+"/"+file 
fantasy_zip.close()
Narendra
  • 1,511
  • 1
  • 10
  • 20
  • Sorry, this code is creating new directory named 'SCHEMAAccountService.json' and again copying the entire directory as before. – kishore Mar 16 '18 at 06:08
  • @kishore edited try now you have to refer directory as "/" like this... – Narendra Mar 16 '18 at 06:10
  • Sorry, it is still complicated now. It creates AccountService.json folder and copies entire directory as previous in that directory. – kishore Mar 16 '18 at 06:14
  • Thank you... but still its copying entire directory still now, please check – kishore Mar 16 '18 at 06:49
  • Your latest edited code and the code I have given in question is generating same output. They were copying the entire directory to target folder. Rather I need only AccountService.json file to be copied into the target folder. – kishore Mar 16 '18 at 06:54
  • @kishore so use file = file.split("/")[-1]...that's it and add it to target_dir – Narendra Mar 16 '18 at 07:02
  • I tried it. If I do that, it was creating the new folder in target directory and again copying the entire directory. Please check – kishore Mar 16 '18 at 07:07