I am trying to write a Python script that will only run processes on a shapefile if there are more than 0 records in it.
I tried to use the Get Count tool (rows = arcpy.GetCount_management(file)) but if you try to run this on an empty shapefile (0 records), it returns:
ERROR 000229: Cannot open [insert shapefile path]
Failed to execute (GetCount).
It only runs on shapefiles that contain records. So what alternate code can I use to check a shapefile has records in it before attempting to run a process?
I have attached the empty shapefile.
Solved! Go to Solution.
Does the other software create the 3 mandatory parts/ extensions:
...
Since you know that it happens on empty datasets, you could wrap this part in a try/except, and manually set the rows to 0 and pass if the exception is thrown. (updated for error filtering)
import arcpy
file = r'test file'
try:
rows = arcpy.GetCount_management(file)
except Exception as err:
rows = 0
# Filter the 000229 error and let any other exceptions that may occur raise.
if 'ERROR 000229' in err.args[0]:
print(f'{file} threw the cannot open file error: {err}')
pass
else:
print(f'{file} threw a different error: {err}')
raise
print(f'rows value is : {rows}')
Here's an exploration of some different methods for counting the number of rows in a table.
The Iterable Cursor: Python Built-ins & Itertools - Esri Community
There must be something else going on. I gave this a test and was able to get "Row Count = 0" just fine on a new empty shapefile. I'm running ArcGIS Pro 2.9.
I actually ran into this same thing yesterday with a new (created as part of a workflow) feature class; I just assumed it was a limitation.
Yeah I've always had this issue for years. My sometimes empty shapefiles are being created with separate software (Alteryx), not using ArcGIS Pro, so maybe it's something to do with that, but the Get Count tool does not like it.
I've had some success running MakeFeatureLayer() on the file first, and then running GetCount().
For sure shapefile writer implementations differ between packages - worst case I've had to handle the error, report with AddWarning and move on.
Unfortunately MakeFeatureLayer() still doesn't work as it just doesn't like an empty shapefile.
Could you elaborate on how you would handle the error? I have two shapefiles and 0, 1, or 2 of them could be empty in this process. I basically want it to perform a process on each one, but if it's empty it currently just throws that error and crashes my script. How could I get it to just continue for the other shapefile, even if the other was empty and errored?
Does the other software create the 3 mandatory parts/ extensions:
...
Since you know that it happens on empty datasets, you could wrap this part in a try/except, and manually set the rows to 0 and pass if the exception is thrown. (updated for error filtering)
import arcpy
file = r'test file'
try:
rows = arcpy.GetCount_management(file)
except Exception as err:
rows = 0
# Filter the 000229 error and let any other exceptions that may occur raise.
if 'ERROR 000229' in err.args[0]:
print(f'{file} threw the cannot open file error: {err}')
pass
else:
print(f'{file} threw a different error: {err}')
raise
print(f'rows value is : {rows}')
Thank you, this is exactly what I was looking for. Yes the shapefile is created with the 3 other mandatory parts.
I tried your code and the exception is picking up the error correctly, it prints "file threw the cannot open file error" but then the next line just returns "Failed to execute (GetCount)" and the script ends without running anything else, so the Pass doesn't appear to be working correctly.
It seems clear there is something in the other shape file writer that ArcGIS does not like, but it is hard to say without you posting a copy of a shape file for people to test.