The code:
import os
import zipfile
import arcpy
def ZipShp (inShp, Delete = True):
"""
Creates a zip file containing the input shapefile
inputs -
inShp: Full path to shapefile to be zipped
Delete: Set to True to delete shapefile files after zip
"""
#List of shapefile file extensions
extensions = [".shp",
".shx",
".dbf",
".sbn",
".sbx",
".fbn",
".fbx",
".ain",
".aih",
".atx",
".ixs",
".mxs",
".prj",
".xml",
".cpg"]
#Directory of shapefile
inLocation = arcpy.Describe (inShp).path
#Base name of shapefile
inName = arcpy.Describe (inShp).baseName
#Create zipfile name
zipfl = os.path.join (inLocation, inName + ".zip")
#Create zipfile object
ZIP = zipfile.ZipFile (zipfl, "w")
#Iterate files in shapefile directory
for fl in os.listdir (inLocation):
#Iterate extensions
for extension in extensions:
#Check if file is shapefile file
if fl == inName + extension:
#Get full path of file
inFile = os.path.join (inLocation, fl)
#Add file to zipfile
ZIP.write (inFile, fl)
break
#Delete shapefile if indicated
if Delete == True:
arcpy.Delete_management (inShp)
#Close zipfile object
ZIP.close()
#Return zipfile full path
return zipfl
And for those of you without the ArcPy module:
import os
import zipfile
def ZipShp (inShp, Delete = True):
"""
Creates a zip file containing the input shapefile
inputs -
inShp: Full path to shapefile to be zipped
Delete: Set to True to delete shapefile files after zip
"""
#List of shapefile file extensions
extensions = [".shp",
".shx",
".dbf",
".sbn",
".sbx",
".fbn",
".fbx",
".ain",
".aih",
".atx",
".ixs",
".mxs",
".prj",
".xml",
".cpg",
".shp.xml"]
#Directory of shapefile
inLocation = os.path.dirname (inShp)
#Base name of shapefile
inName = os.path.basename (os.path.splitext (inShp)[0])
#Create zipfile name
zipfl = os.path.join (inLocation, inName + ".zip")
#Create zipfile object
ZIP = zipfile.ZipFile (zipfl, "w")
#Empty list to store files to delete
delFiles = []
#Iterate files in shapefile directory
for fl in os.listdir (inLocation):
#Iterate extensions
for extension in extensions:
#Check if file is shapefile file
if fl == inName + extension:
#Get full path of file
inFile = os.path.join (inLocation, fl)
#Add file to delete files list
delFiles += [inFile]
#Add file to zipfile
ZIP.write (inFile, fl)
break
#Delete shapefile if indicated
if Delete == True:
for fl in delFiles:
os.remove (fl)
#Close zipfile object
ZIP.close()
#Return zipfile full path
return zipfl
That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.
ReplyDeletepython online training
Yes, that’s the way I always wanted to come over such a wonderful platform where I could satisfy myself regarding my issues. I found answers of all most of my check list I prepared after having a lot of confusion. Great job.Dell Network N Series POE
ReplyDeleteThis is an amazing little function. Thanks so much for sharing!
ReplyDelete