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
Solved! Go to Solution.
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;
Are you trying to zip featureclasses in the geodatabase or shapefiles? I don't see arcpy.ListFeatureclasses anywhere
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;
There wasn't any lock files in there I hope
That ol' chestnut. Thankfully, whoever wrote the sample code I used took that into consideration and they're filtered out!