Neeraj Singh
Neeraj Singh

Reputation: 5

shutil copy issue

So I have the following script

# Import system modules
import arcpy, os
import fnmatch
import shutil
import zipfile

zipf = zipfile.ZipFile('MXD_DC.zip', 'w', zipfile.ZIP_DEFLATED)
    for root, dirs, files in os.walk(r"Y:\Data\MXD_DC"):
       for file in files:
            zipf.write(os.path.join(root, file))

 shutil.copy(r'MXD_DC.zip', 'D:/')

After copying the file over to d drive when I try to unzip it, the error is "Before you can extract files, you must copy files to this compressed zipped folder". I can take the original zip file from the other drive and unzip it just fine. I can manually copy it over to d drive and unzip it just fine. It happens only when I use shutil to copy to the d drive.

Upvotes: 0

Views: 410

Answers (1)

njoosse
njoosse

Reputation: 549

You need to close the zipfile before you go to copy it. Either zipf.close() before the shutil.copy or

with zipfile.ZipFile('MXD_DC.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    for root, dirs, files in os.walk(r"Y:\Data\MXD_DC"):
        for file in files:
            zipf.write(os.path.join(root, file))

shutil.copy2('MXD_DC.zip','D:/')

You could also use shutil.copy2 again.

Upvotes: 1

Related Questions