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.
I've attached the empty shapefile now.
Looking at the attached shape file *.shp file and comparing it to an new, empty shape file *.shp file from ArcGIS Pro, there are 3 fields that are different (see ESRI Shapefile Technical Description)
I really doubt the discrepancy in the bounding box values is causing the issue, it is most likely ArcGIS Pro (I haven't tested ArcMap) doesn't like the "Null Shape" for the shape type.
@JoshuaBixbycould you describe how you did this inspection?
I had this exact problem yesterday - where shapefile writer implementations didn't play well between several programs. It'd be great community knowledge to have because this issue almost always expresses itself indirectly (like in OP's case).
You have to start with the Shapefile Technical Description, which I linked to in my earlier comment. That describes the binary structure of the file contents. From there, you have to open the file and read the raw contents. There are several tools for doing such work. I happened to use PowerShell to read the file in and then used BitConverter Class (System) | Microsoft Docs to do the conversion.
Also interesting, QGIS prompts "Select Items to Add", and shows the empty Unknown data in OP's sample if you add it to a map.
Simple ogr2ogr like
ogr2ogr -f "ESRI Shapefile" .../EmptyShapefile_ogr.shp .../EmptyShapefile.shp
forced the input to Linestring and set the extents both to 0.0. QGIS opened that file with no prompt.
So, multiple ways to handle this. Might be worth talking to Alteryx as well.
Maybe try "continue" instead? It'll pass to the next item in the for() loop.
Bummer! Maybe try BaseException instead of Exception. ... You can see what type of exception is being thrown in the debugger and you can use that type to target the exception that is being thrown. For my testing, I'm getting a 000732 ExecuteError on a file that it cant open and 000229 may be some other exception type.
Note that printing 'err' will print all of the exception including the Failed to execute (GetCount). as a way to let you know that file failed... you can remove it if you want. If its catching and passing correctly (pass isnt really needed in there), you should see the rows value is: x printed.