1

I made a Python-Addin-Button with the function to create a zip. Everything works fine, but after creating the zip-file, the zipfile is locked, when the mxd-Project is open. Any suggestions why I can't open or delete the zipfile?

Here my script:

    import sys, os, arcpy, zipfile, traceback, pythonaddins
    from arcpy import *

    class CreateZIP(object):
        """Implementation for Python_Addins_addin.button (Button)"""
        def __init__(self):
            self.enabled = True
            self.checked = False
        def onClick(self):
            arcpy.env.overwriteOutput = True
            mxd = arcpy.mapping.MapDocument('current')
            mxdpath = mxd.filePath
            inWorkspace = os.path.dirname(mxdpath)
            outWorkspace = inWorkspace
            arcpy.env.workspace = inWorkspace
            outzip = zipfile.ZipFile(outWorkspace+"\Arch_Data.zip","a",zipfile.ZIP_DEFLATED)
            infolder = os.path.join(inWorkspace,"DATA")
            path = os.path.normpath(infolder)
            for (dirpath, dirnames, filenames) in os.walk(path):
                for file in filenames:
                    if not file.endswith(".lock"):
                            outzip.write(os.path.join(dirpath, file),os.path.join(os.path.basename(path),
                                                              os.path.join(dirpath, file)[len(path)+len(os.sep):]))
            outzip.close()                
            z = zipfile.ZipFile("Arch_Data.zip","a")
            z.write("README.txt")
            outzip.close()
  • I call your attention to the fact it is not necessary to use the workspace. os.getcwd() = os.path.dirname(mxdpath). Also if you have not pre-registered in your mxd, the path will be empty. In this case mxdpath=u"" because MapDocument is in memory outWorkspace+"\Arch_Data.zip" can be directly Arch_Data.zip if you create zip in same folder as your document because it use os.getcwd() as basepath – GeoStoneMarten Dec 2 '15 at 10:28
1

Because you open zipfile twice and clause only one

        outzip.close()                
        z = zipfile.ZipFile("Arch_Data.zip","a")
        z.write("README.txt")
        # outzip.close()# This is Not Good!!!
        z.close()  # Check with this
0

Thank you for your help and suggestions. As well I added a check to test, if the readme, that I append to the zipfolder exists. Now the Add-in-button works fine:

def onClick(self):
        try:
            arcpy.env.overwriteOutput = True
            mxd = arcpy.mapping.MapDocument('current')
            df = arcpy.mapping.ListDataFrames(mxd)[0]
            mxdpath = mxd.filePath
            inWorkspace = os.path.dirname(mxdpath)
            outzip = zipfile.ZipFile("Arch_Data.zip","a",zipfile.ZIP_DEFLATED)
            infolder = os.path.join(inWorkspace,"DATA")
            path = os.path.normpath(infolder)
            for (dirpath, dirnames, filenames) in os.walk(path):
                for file in filenames:
                    if not file.endswith(".lock"):
                            outzip.write(os.path.join(dirpath, file),os.path.join(os.path.basename(path),
                                                                  os.path.join(dirpath, file)[len(path)+len(os.sep):]))
            outzip.close()                
            z = zipfile.ZipFile("Arch_Data.zip","a")
            if os.path.isfile("README.txt"):  
                z.write("README.txt")
                z.close()
            else:
                pythonaddins.MessageBox("Don't forget to add the readme.","Info",0)  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.