Is it possible to retrieve the size of a shapefile using python and arcpy? If so, how?
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) #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) 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) if not ext in extensions: continue #get size flFullPath = os.path.join (shpDir, fl) size += os.path.getsize (flFullPath) return size
You can use a generator expression to efficiently find the shapefile size (i.e. including all associated files). The following method uses built-in Python 2.7 functionality.
import os, glob ws = r'C:\path\to\your\shapefiles' # input workspace shapefiles = glob.glob(os.path.join(ws, "*.shp")) # List all .shp files for shapefile in shapefiles: s = sum([os.stat(x).st_size for x in glob.glob(shapefile.split(".") + "*")]) print "The file size for %s is %s bytes or %s kb" % (shapefile, s, (float(s)/1000))
The generator expression does the following:
- List all of the associated files of a shapefile. In this case, strip
off the ".shp" extension from the path and use
globwith the path and wildcard
*to list all of the associated files
- Get the file sizes in bytes using
- Sum them using the generator