Select to view content in your preferred language

Adding extra files to a zip?

2792
2
01-19-2016 05:46 AM
JemmaWindow
New Contributor

So I got this fantastic script from: Script for Making Individual Zip file for Each Shapefile? but I required some extra files to be added to each of the individual zips, e.g. 2 pdfs: one with metadata and another for terms and conditions. 

import arcgisscripting, os, glob, zipfile, shutil

from os import path as p

# Create the Geoprocessor object

gp = arcgisscripting.create()

# arcpy.overwriteOutput = True

gp.overwriteoutput=1

def ZipShapes(path, out_path):

    gp.workspace = path

    shapes = gp.ListFeatureClasses("*")

    # iterate through list of shapefile

    for shape in iter(shapes.next, None):

        name = p.splitext(shape)[0]

        print name

        zip_path = p.join(out_path, name + '.zip')

        zip = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)

        os.chdir(path)

        for files in glob.glob('%s.*' %name):

            zip.write(p.join(path,files), files)

        print 'All files written to %s' %zip_path

        # copy pdf files to zip folder

        for f in pdfList:

            shutil.copyfile(os.path.join(os.path.dirname(path),f), os.path.join(path,f))

        zip.close()

if __name__ == '__main__':

    path = r'S:\Species\MARINEFISH'

    outpath = r'S:\Species\MARINEFISH\ZIPS'

    pdfList =["Terms & Conditions of Use.pdf", "METADATA.pdf"]

    ZipShapes(path, outpath)

I keep getting the error:

Traceback (most recent call last):

  File "S:\scripts\Python\Zipping_Shapefiles", line 34, in <module>

    ZipShapes(path, outpath)

  File "S:\scripts\Python\Zipping_Shapefiles", line 25, in ZipShapes

    shutil.copyfile(os.path.join(os.path.dirname(path),f), os.path.join(path,f))

  File "C:\Python27\ArcGIS10.3\lib\shutil.py", line 82, in copyfile

    with open(src, 'rb') as fsrc:

IOError: [Errno 2] No such file or directory: 'S:\\Species\\METADATA.pdf'

Any help would be greatly appreciated!

0 Kudos
2 Replies
LukeSturtevant
Occasional Contributor III

Did you already look at the obvious issue of 'S:\\Species\\METADATA.pdf' not actually existing? Where is the metadata file located?

Also it looks like you are trying to copy your pdfs to the wrong place? os.path.join(path,f) ​would attempt to copy the pdfs to 'S:\Species\MARINEFISH\ [PDFName]'

RebeccaStrauch__GISP
MVP Emeritus

Assuming this is your own data that you are trying to .zip, and the script is one you received from someone else.....do you even have a "term of use" and "metadata" file created?  It is good to have these items before you distribute data, as a general business practice, but if you don't you can either created the files now, or comment out that line.  but I would suggest at least writing up minimal terms and metadata and saving as PDF in the path shown.

btw...I'm assuming your data is in the same location.