Help with Exit Code 0 issue

917
4
Jump to solution
03-06-2019 09:04 PM
LindsayRaabe_FPCWA
Occasional Contributor III

I have a basic python code that is intended to take the contents of a File GDB and ZIP them (not that actual .gdb itself though). When I test the code it says it's all good. When I run the code however, it goes straight to "Exit Code 0" and nothing happens. No new ZIP file, nothing. Any ideas where I'm going wrong?

import os
import shutil
import zipfile

# Creates a zip file containing the input shapefile
#   inFileGDB: Full path to FileGDB folder to be zipped
#   Delete: Set to True to delete fileGDB files after zip

# Creates a zip file containing the contents of a filegeodatabase
def Zipfgdb(inFileGDB = r'\\my\data\pathway\MyData.gdb', Delete = 'False'):
    print inFileGDB + " : Delete original files = " + Delete
    #Directory of file geodatabase
    inLocation = os.path.dirname (inFileGDB)
    print "inLocation: " + inLocation
    #Base name of shapefile
    inName = os.path.basename (os.path.splitext(inFileGDB)[0])
    print "inName: " + inName
    #Create the zipfile name 
    zipfl = os.path.join (inLocation, inName + ".zip")
    print "New ZIP file: " + zipfl
    #Create zipfile object
    ZIP = zipfile.ZipFile (zipfl, "w")
    print "ZIP file created"
    #Iterate files in shapefile directory
    for fl in os.listdir (inFileGDB):
        #Get full path of file
        inFile = os.path.join (inFileGDB, fl)
        #Add file to zipfile. exclude any lock files
        if os.path.splitext(fl)[1][1:] <> 'lock':
            ZIP.write(inFile,fl)
        print "files added to ZIP file"
    #Delete filegeodatabase if indicated
    if Delete == True:
        shutil.rmtree(inFileGDB)
        print "Original file deleted"
    #Close zipfile object
    ZIP.close()
    #Return zipfile full path
    return zipfl
Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
1 Solution

Accepted Solutions
LindsayRaabe_FPCWA
Occasional Contributor III

Hi Dan. Not exactly. The GDB does contain a single feature class, though what this script is intended to do is zip all of the files within a gdb as if you were viewing it through windows explorer (i.e. below). A colleague has helped out and found the solution was with the Return call at the end being incomplete. It should have looked like this [Zipfgdb() as the last line];

#Close zipfile object
    ZIP.close()
    #Return zipfile full path
    return zipfl
Zipfgdb()

GDB;

ZIP;

Lindsay Raabe
GIS Officer
Forest Products Commission WA

View solution in original post

0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

Are you trying to zip featureclasses in the geodatabase or shapefiles? I don't see arcpy.ListFeatureclasses anywhere

0 Kudos
LindsayRaabe_FPCWA
Occasional Contributor III

Hi Dan. Not exactly. The GDB does contain a single feature class, though what this script is intended to do is zip all of the files within a gdb as if you were viewing it through windows explorer (i.e. below). A colleague has helped out and found the solution was with the Return call at the end being incomplete. It should have looked like this [Zipfgdb() as the last line];

#Close zipfile object
    ZIP.close()
    #Return zipfile full path
    return zipfl
Zipfgdb()

GDB;

ZIP;

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
DanPatterson_Retired
MVP Emeritus

There wasn't any lock files in there I hope

0 Kudos
LindsayRaabe_FPCWA
Occasional Contributor III

That ol' chestnut. Thankfully, whoever wrote the sample code I used took that into consideration and they're filtered out!

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos