Select to view content in your preferred language

Counting records in a shapefile that has 0 records

1780
16
Jump to solution
05-03-2022 09:16 AM
ASw93
by
Occasional Contributor

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.

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Does the other software create the 3 mandatory parts/ extensions:

  • shp -- Main file (mandatory); a direct access, variable-record-length file in which each record describes a shape with a list of its vertices.
  • shx -- Index file (mandatory). In the index file, each record contains the offset of the corresponding main file record from the beginning of the main file. The index file (.shx) contains a 100-byte header followed by 8-byte, fixed-length records.
  • dbf -- dBASE Table file (mandatory); a constrained form of DBF that contains feature attributes with one record per feature. The one-to-one relationship between geometry and attributes is based on record number. Attribute records in the dBASE file must be in the same order as records in the main file

          ...

  • sbn -- Part 1 of spatial index for read-write instances of the Shapefile format. If present, essential for correct processing.
  • sbx -- Part 2 of spatial index for read-write instances of the Shapefile format. If present, essential for correct processing

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}')

 

 

 

View solution in original post

16 Replies
BlakeTerhune
MVP Regular Contributor

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

DannyMcVey
Esri Contributor

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.

 

AlfredBaldenweck
MVP Regular Contributor

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.

ASw93
by
Occasional Contributor

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.

0 Kudos
by Anonymous User
Not applicable

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.

ASw93
by
Occasional Contributor

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?

0 Kudos
by Anonymous User
Not applicable

Does the other software create the 3 mandatory parts/ extensions:

  • shp -- Main file (mandatory); a direct access, variable-record-length file in which each record describes a shape with a list of its vertices.
  • shx -- Index file (mandatory). In the index file, each record contains the offset of the corresponding main file record from the beginning of the main file. The index file (.shx) contains a 100-byte header followed by 8-byte, fixed-length records.
  • dbf -- dBASE Table file (mandatory); a constrained form of DBF that contains feature attributes with one record per feature. The one-to-one relationship between geometry and attributes is based on record number. Attribute records in the dBASE file must be in the same order as records in the main file

          ...

  • sbn -- Part 1 of spatial index for read-write instances of the Shapefile format. If present, essential for correct processing.
  • sbx -- Part 2 of spatial index for read-write instances of the Shapefile format. If present, essential for correct processing

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}')

 

 

 

ASw93
by
Occasional Contributor

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.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos