I wonder how to get size of disk space that a specific vector dataset occupies on a disk. I didn't find any ogr2ogr util or OGR Python method that can return a list of files that belongs to given dataset. I could use ogr2ogr util or CopyDataSource OGR method to copy the dataset to new empty directory using the same driver and analyze the directory then but this method do not preserve original features (and file size) or even does not allow dataset creation for some of OGR formats. Another way is to make a list of possible file extensions that may belongs to given vector format and try to test if the file exists... Any other ideas?
1 Answer
The problem is that shapefiles have only 3 mandatory files and ogr or other Python modules recognize only those (+ eventually the .prj file)
.shp
.dbf
.shx
Many applications as ArcGIS or QGIS add other specific files, not recognized by the modules.
You need to use an artifice as in Retrieving size of shapefile in ArcPy? but the use of a list of extensions as in the script presented is too limited in scope (only for ArcGis).
A proposition, for example
import glob,os
shapefiles = [file for file in glob.glob("*.shp")]
# name of the first shapefile (without extension)
name = os.path.splitext(shapefiles[0])[0]
print name
'test' # test.shp
# search string
search = '*'+ name + '*.*'
for file in glob.glob(search):
print file, os.path.getsize(file)
test.dbf 176
test.prj 497
test.qpj 783
test.shp 268
test.shx 108
# total size in bytes
print sum([os.path.getsize(file) for file in glob.glob(search)])
1832
new
With a filter
extensions = [".shp", ".shx",".dbf"] # without .qpg and .prj
for file in glob.glob(search):
if os.path.splitext(file)[1] in extensions:
print file, os.path.getsize(file)
test.dbf 176
test.shp 268
test.shx 108
print sum([os.path.getsize(file) for file in glob.glob(search) if os.path.splitext(file)[1] in extensions])
552
-
Thanks for the example. Unfortunately it does not solve case when beside the Shapefile files (test.shp, ...) exist for example another dataset having the same name (e.g. test.gml) - then I get size also of another dataset(s)...– TonnyCommented Dec 29, 2015 at 10:33
-
Then you can use a filter with a list of extensions as in Retrieving size of shapefile in ArcPy?– geneCommented Dec 29, 2015 at 10:52
-
-