import zipfile, sys, os, glob # Set the folder that contains the ShapeFiles myFolder = "C:/Scratch/FWS/" def zipShapefile(myFolder): # Check if folder exists if not (os.path.exists(myFolder)): print myFolder + ' Does Not Exist!' return False # Get a list of shapefiles ListOfShapeFiles = glob.glob(myFolder + '*.shp') # Main shapefile loop for sf in ListOfShapeFiles: print 'Zipping ' + sf # Create an output zip file name from shapefile newZipFN = sf[:-3] + 'zip' # Check if output zipfile exists, delete it if (os.path.exists(newZipFN)): print 'Deleting '+newZipFN os.remove(newZipFN) if (os.path.exists(newZipFN)): print 'Unable to Delete' + newZipFN return False # Create zip file object zipobj = zipfile.ZipFile(newZipFN,'w') # Cycle through all associated files for shapefile adding them to zip file for infile in glob.glob( sf.lower().replace(".shp",".*")): print 'zipping ' + infile + ' into ' + newZipFN if infile.lower() != newZipFN.lower() : # Avoid zipping the zip file! zipobj.write(infile,os.path.basename(infile),zipfile.ZIP_DEFLATED) # Close zipfile print 'ShapeFile zipped!' zipobj.close() # Got here so everything is OK return True # Call function to zip files b = zipShapefile(myFolder) if b: print "Zipping done!" else: print "An error occurred during zipping."
import zipfile, sys, os, glob def zipShapefilesInDir(inDir, outDir): # Check that input directory exists if not os.path.exists(inDir): print "Input directory %s does not exist!" % inDir return False # Check that output directory exists if not os.path.exists(outDir): # Create it if it does not print "Creating output directory %s" %outDir os.mkdir(outDir) print "Zipping shapefile(s) in folder %s to output folder %s" % (inDir, outDir) # Loop through shapefiles in input directory for inShp in glob.glob(os.path.join(inDir, "*.shp")): # Build the filename of the output zip file outZip = os.path.join(outDir, os.path.splitext(os.path.basename(inShp))[0] + ".zip") # Zip the shapefile zipShapefile(inShp, outZip) return True def zipShapefile(inShapefile, newZipFN): print 'Starting to Zip '+(inShapefile)+' to '+(newZipFN) # Check that input shapefile exists if not (os.path.exists(inShapefile)): print inShapefile + ' Does Not Exist' return False # Delete output zipfile if it already exists if (os.path.exists(newZipFN)): print 'Deleting '+newZipFN os.remove(newZipFN) # Output zipfile still exists, exit if (os.path.exists(newZipFN)): print 'Unable to Delete'+newZipFN return False # Open zip file object zipobj = zipfile.ZipFile(newZipFN,'w') # Loop through shapefile components for infile in glob.glob( inShapefile.lower().replace(".shp",".*")): # Skip .zip file extension if os.path.splitext(infile)[1].lower() != ".zip": print "Zipping %s" % (infile) # Zip the shapefile component zipobj.write(infile,os.path.basename(infile),zipfile.ZIP_DEFLATED) # Close the zip file object zipobj.close() return True # Main method, used when this script is the calling module, otherwise # you can import this module and call your functions from other modules if __name__=="__main__": ## testShapeFile = r"C:\temp\geomTest.shp" ## testZipFile = r"C:\temp\geomTest.zip" ## zipShapefile(testShapeFile,testZipFile) inDir = r"c:\temp" outDir = r"c:\temp\testZipShp" zipShapefilesInDir(inDir, outDir) print "done!"
I should have held off another 10 months - marked the 10 year anniversary of this post. It worked!!! Just changed the print statements into
fav_fruit = 'cantalope'
print('my favorite fruit is {}'.format(fav_fruit))
And good to go --> Python 3.6
I hope by now you've found your answer, but if not, or someone else needs to know:
Go to 'Geoprocessing' up on the main menu (File, Edit, View, Bookmarks...) bar, when it drops down, go to 'python' and click on it... here is where you will paste your code...
Thank you both for your help they work great!