<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Counting records in a shapefile that has 0 records in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170298#M64469</link>
    <description>&lt;P&gt;I've had some success running MakeFeatureLayer() on the file first, and then running GetCount().&lt;/P&gt;&lt;P&gt;For sure shapefile writer implementations differ between packages - worst case I've had to handle the error, report with AddWarning and move on.&lt;/P&gt;</description>
    <pubDate>Tue, 03 May 2022 18:54:01 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2022-05-03T18:54:01Z</dc:date>
    <item>
      <title>Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170208#M64464</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;I tried to use the Get Count tool (&lt;EM&gt;rows =&amp;nbsp;arcpy.GetCount_management(file)&lt;/EM&gt;) but if you try to run this on an empty shapefile (0 records), it returns:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;ERROR 000229: Cannot open [insert shapefile path]
Failed to execute (GetCount).&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;I have attached the empty shapefile.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 15:08:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170208#M64464</guid>
      <dc:creator>ASw93</dc:creator>
      <dc:date>2022-05-04T15:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170214#M64465</link>
      <description>&lt;P&gt;Here's an exploration of some different methods for counting the number of rows in a table.&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/gis-blog/the-iterable-cursor-python-built-ins-itertools/ba-p/896458" target="_blank"&gt;The Iterable Cursor: Python Built-ins &amp;amp; Itertools - Esri Community&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2022 16:30:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170214#M64465</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2022-05-03T16:30:20Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170215#M64466</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2022 16:30:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170215#M64466</guid>
      <dc:creator>DannyMcVey</dc:creator>
      <dc:date>2022-05-03T16:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170218#M64467</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2022 16:42:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170218#M64467</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2022-05-03T16:42:46Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170223#M64468</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2022 16:54:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170223#M64468</guid>
      <dc:creator>ASw93</dc:creator>
      <dc:date>2022-05-03T16:54:00Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170298#M64469</link>
      <description>&lt;P&gt;I've had some success running MakeFeatureLayer() on the file first, and then running GetCount().&lt;/P&gt;&lt;P&gt;For sure shapefile writer implementations differ between packages - worst case I've had to handle the error, report with AddWarning and move on.&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2022 18:54:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170298#M64469</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-05-03T18:54:01Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170530#M64480</link>
      <description>&lt;P&gt;Unfortunately MakeFeatureLayer() still doesn't work as it just doesn't like an empty shapefile.&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 09:47:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170530#M64480</guid>
      <dc:creator>ASw93</dc:creator>
      <dc:date>2022-05-04T09:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170568#M64482</link>
      <description>&lt;P&gt;Does the other software create the 3 mandatory parts/ extensions:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;I&gt;shp&lt;/I&gt; -- Main file (mandatory); a direct access, variable-record-length file in which each record describes a shape with a list of its vertices.&lt;/LI&gt;&lt;LI&gt;&lt;I&gt;shx&lt;/I&gt; -- 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.&lt;/LI&gt;&lt;LI&gt;&lt;I&gt;dbf&lt;/I&gt; -- dBASE Table file (mandatory); a constrained form of &lt;A href="https://www.loc.gov/preservation/digital/formats/fdd/fdd000325.shtml" target="_blank" rel="noopener"&gt;DBF&lt;/A&gt; 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&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;I&gt;sbn&lt;/I&gt; -- Part 1 of spatial index for read-write instances of the Shapefile format. If present, essential for correct processing.&lt;/LI&gt;&lt;LI&gt;&lt;I&gt;sbx&lt;/I&gt; -- Part 2 of spatial index for read-write instances of the Shapefile format. If present, essential for correct processing&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;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)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;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}')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 13:16:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170568#M64482</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-05-04T13:16:36Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170592#M64483</link>
      <description>&lt;P&gt;Thank you, this is exactly what I was looking for. Yes the shapefile is created with the 3 other mandatory parts.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 13:58:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170592#M64483</guid>
      <dc:creator>ASw93</dc:creator>
      <dc:date>2022-05-04T13:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170641#M64489</link>
      <description>&lt;P&gt;Bummer!&amp;nbsp; Maybe try &lt;SPAN class=""&gt;BaseException instead of Exception.&amp;nbsp; ... 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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;Note that printing 'err' will print all of the exception including the &lt;EM&gt;Failed to execute (GetCount)&lt;/EM&gt;. 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 &lt;EM&gt;rows value is:&amp;nbsp; x&lt;/EM&gt; printed.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 15:03:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170641#M64489</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-05-04T15:03:11Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170642#M64490</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 15:04:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170642#M64490</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-05-04T15:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170644#M64491</link>
      <description>&lt;P&gt;I've attached the empty shapefile now.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 15:10:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170644#M64491</guid>
      <dc:creator>ASw93</dc:creator>
      <dc:date>2022-05-04T15:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170685#M64497</link>
      <description>&lt;P&gt;Maybe try "continue" instead? It'll pass to the next item in the for() loop.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 16:23:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170685#M64497</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2022-05-04T16:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170796#M64500</link>
      <description>&lt;P&gt;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 &lt;A href="https://www.esri.com/content/dam/esrisites/sitecore-archive/Files/Pdfs/library/whitepapers/pdfs/shapefile.pdf" target="_blank"&gt;ESRI Shapefile Technical Description&lt;/A&gt;)&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Byte 32 - Shape Type is "Null Shape" in the attached but "Polygon" in the new Pro&lt;/LI&gt;&lt;LI&gt;Byte 36 - Bounding Box XMin is 1.0 in attached but 0.0 in new Pro&lt;/LI&gt;&lt;LI&gt;Byte 44 - Bounding Box YMin is 1.0 in attached but 0.0 in new Pro&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 19:24:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170796#M64500</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-05-04T19:24:58Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170834#M64501</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/1371"&gt;@JoshuaBixby&lt;/a&gt;could you describe how you did this inspection?&lt;BR /&gt;&lt;BR /&gt;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).&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 20:13:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170834#M64501</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-05-04T20:13:39Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170838#M64502</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Simple ogr2ogr like&lt;/P&gt;&lt;LI-CODE lang="c"&gt;ogr2ogr -f "ESRI Shapefile" .../EmptyShapefile_ogr.shp .../EmptyShapefile.shp&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;forced the input to Linestring and set the extents both to 0.0. QGIS opened that file with no prompt.&lt;BR /&gt;&lt;BR /&gt;So, multiple ways to handle this. Might be worth talking to Alteryx as well.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 20:29:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170838#M64502</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-05-04T20:29:40Z</dc:date>
    </item>
    <item>
      <title>Re: Counting records in a shapefile that has 0 records</title>
      <link>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170853#M64503</link>
      <description>&lt;P&gt;You have to start with the Shapefile Technical Description, which I linked to in my earlier comment.&amp;nbsp; That describes the binary structure of the file contents.&amp;nbsp; From there, you have to open the file and read the raw contents.&amp;nbsp; There are several tools for doing such work.&amp;nbsp; I happened to use PowerShell to read the file in and then used &lt;A href="https://docs.microsoft.com/en-us/dotnet/api/system.bitconverter?view=net-6.0" target="_blank"&gt;BitConverter Class (System) | Microsoft Docs&lt;/A&gt; to do the conversion.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 21:06:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-records-in-a-shapefile-that-has-0-records/m-p/1170853#M64503</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-05-04T21:06:48Z</dc:date>
    </item>
  </channel>
</rss>

