How to programmatically check if the number of shapes = number of table records?
From the sound of your question, it seems like all you really want to do is determine whether or not a shapefile has issues with it (in this case, mismatched records). If you just need to identify those with issues, you don't actually need to count the records in the DBF and Shapefile to determine if it is in error. Here's why:
If you try to run the GetCount function on a shapefile that has different record counts, it will fail with the error:
ERROR 000229: Cannot open . Failed to execute (GetCount).
Since the GetCount function fails in this scenario, and all you want to do is identify the shapefiles in error, you can catch this with a try/except clause in your code, instead of the if/else you were previously attempting to use.
I took the liberty of adding the "List FeatureClasses" code and loop so that you could test all the FC's in your workspace without manually having to test each one.
# Import system modules
import arcpy
from arcpy import env
env.workspace = "C:/data"
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
try:
result_dbf = int(arcpy.GetCount_management(fc).getOutput(0))
print fc + ": " + str(result_dbf) + " records"
except:
print "There is a problem with: " + str(fc)
What about using pyshp? I installed it with pip and what I tried below is pretty much straight out of the README:
>>> import shapefile
>>> sf = shapefile.Reader("/Users/chad/CoalOutcrops.shp")
>>> shapes = sf.shapes()
>>> len(shapes)
33732
>>> records = sf.records()
>>> len(records)
33732
>>>
Unfortunately (or maybe fortunately?) I don't have any jacked-up shapefiles to test to see if no. of shapes can != no. of records.
Wait just a minute, I do now have a jacked up shapefile thanks to Kirk's idea in the comments below. I backed up the dbf, made a copy of the entire shapefile, deleted some features, then renamed the backed-up dbf back to the original, and lo and behold, the number of shapes < number of records:
>>> sf = shapefile.Reader("/Users/chad/CoalOutcrops.shp")
>>> records = sf.records()
>>> len(records)
33732
>>> shapes = sf.shapes()
>>> len(shapes)
33721
>>>
The shapefile format is documented. I would guess the number of records in the shp file does not correspond to the number of records in the dbf file.
The shp file format is documented here. So you could write a program to count the number of shapes. The dbf format is documented in many places and you should be able to find samples for counting rows, e.g. here.