Monday, 21 October 2019

Retrieving size of shapefile in ArcPy?


Is it possible to retrieve the size of a shapefile using python and arcpy? If so, how?



Answer



Iterate through all files in the shapefile directory with a valid shapefile extension and add their sizes together. The os module is helpful for this task. Here's a function that returns the size of all shapefile files associated with an input shapefile in bytes. Use the full path of the shapefile as your input.


import os


def ShpSize (inShp):
#standardize lowercase
inShp = inShp.lower ()
#shapefile extensions
extensions = [".shp",
".shx",
".dbf",
".sbn",
".sbx",
".fbn",

".fbx",
".ain",
".aih",
".atx",
".ixs",
".mxs",
".prj",
".xml",
".cpg"]


#shape file name without directory
shpName = os.path.basename (inShp)
#shape file name without .shp extension
shpFlName = os.path.splitext(shpName)[0]

#size set to zero
size = 0
#directory of shapefile
shpDir = os.path.dirname (inShp)
#iterate directory files

for fl in os.listdir (shpDir):
#standardize lowercase
fl = fl.lower ()
#skip file names that don't match shapefile
flName = os.path.splitext(fl)[0]
if not flName == shpFlName:
#special case: .shp.xml file
if not fl == shpFlName + ".shp.xml":
continue
#skip file names without proper extension

ext = os.path.splitext(fl)[1]
if not ext in extensions:
continue
#get size
flFullPath = os.path.join (shpDir, fl)
size += os.path.getsize (flFullPath)

return size

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...