<?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: ListFields after GetCount throws error in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/listfields-after-getcount-throws-error/m-p/1659655#M74798</link>
    <description>&lt;P&gt;If you want speed, you can get a big bump by not getting the count, but instead just trying to iterate a cursor and immediately returning True if there's rows. Couple that with Dan's isFeatureLayer check and you can do everything in a pretty compact filter operation:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

def has_rows(lyr: arcpy.mp.Layer) -&amp;gt; bool:
    if not lyr.isFeatureLayer:
        return False
    with arcpy.da.SearchCursor(lyr, 'OBJECTID') as cur:
        for _ in cur:
            return True
    return False


def get_field_names(map: arcpy.mp.Map) -&amp;gt; None:
    for lyr in filter(has_rows, map.listLayers()):
        for field in arcpy.ListFields(lyr):
            print(field.name)
            
if __name__ == '__main__':
    aprx = '&amp;lt;project&amp;gt;.aprx'
    m = arcpy.mp.ArcGISProject(aprx).listMaps()[0]
    get_field_names(m)
    &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have broken layers in a map, you can also make sure that the feature layer has a valid datasource:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def has_rows(lyr: arcpy.mp.Layer) -&amp;gt; bool:
    if not lyr.isFeatureLayer or lyr.isBroken:
        return False
    with arcpy.da.SearchCursor(lyr, 'OBJECTID') as cur:
        for _ in cur:
            return True
    return False&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 22 Oct 2025 15:18:52 GMT</pubDate>
    <dc:creator>HaydenWelch</dc:creator>
    <dc:date>2025-10-22T15:18:52Z</dc:date>
    <item>
      <title>ListFields after GetCount throws error</title>
      <link>https://community.esri.com/t5/python-questions/listfields-after-getcount-throws-error/m-p/1659349#M74793</link>
      <description>&lt;P&gt;I'm trying to loop through all fields in all Layers in a Pro Project and am getting a weird error that only happens when I first check for data in the Layer.&amp;nbsp;&lt;FONT color="#FF0000"&gt;&lt;BR /&gt;&lt;/FONT&gt;&lt;BR /&gt;This code works but unnecessarily (for my purposes) loops through all fields when the Layer is empty (slow):&lt;/P&gt;&lt;LI-CODE lang="python"&gt;aprx = fullPathToProProject.aprx
m = (arcpy.mp.ArcGISProject(aprx)).listMaps()[0]
for lyr in m.listLayers:
    for field in arcpy.ListFields(lyr):
        print(field.name)&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;This skips Layers with no data (much faster) but fails if the Layer actually has data:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;aprx = fullPathToProProject.aprx
m = (arcpy.mp.ArcGISProject(aprx)).listMaps()[0]
for lyr in m.listLayers:
    if int(arcpy.management.GetCount(lyr).getOutput(0)) &amp;gt; 0:
        for field in arcpy.ListFields(lyr):
            print(field.name)&lt;/LI-CODE&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;Error Message:&amp;nbsp;&lt;/FONT&gt;&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;fullPythonFilePath&amp;gt;.py", line 5, in &amp;lt;module&amp;gt;&lt;BR /&gt;for field in arcpy.ListFields(lyr):&lt;BR /&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py", line 1228, in ListFields&lt;BR /&gt;return gp.listFields(dataset, wild_card, field_type)&lt;BR /&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 378, in listFields&lt;BR /&gt;self._gp.ListFields(*gp_fixargs(args, True)))&lt;BR /&gt;OSError: "&amp;lt;layerName&amp;gt;" does not exist&lt;BR /&gt;&lt;BR /&gt;&lt;/FONT&gt;Any suggestions on testing for data and then listing fields?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Oct 2025 16:05:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/listfields-after-getcount-throws-error/m-p/1659349#M74793</guid>
      <dc:creator>TrevorWeiland</dc:creator>
      <dc:date>2025-10-21T16:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: ListFields after GetCount throws error</title>
      <link>https://community.esri.com/t5/python-questions/listfields-after-getcount-throws-error/m-p/1659474#M74794</link>
      <description>&lt;P&gt;I would adjust your code to check to ensure that you are working with FeatureClasses just in case you have a layer that doesn't the requirements for listFields&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/layer-class.htm" target="_blank"&gt;Layer—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;using&amp;nbsp;&lt;EM&gt;isFeatureLayer&amp;nbsp;&lt;/EM&gt;between lines 3 and 4&lt;/P&gt;</description>
      <pubDate>Tue, 21 Oct 2025 21:32:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/listfields-after-getcount-throws-error/m-p/1659474#M74794</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2025-10-21T21:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: ListFields after GetCount throws error</title>
      <link>https://community.esri.com/t5/python-questions/listfields-after-getcount-throws-error/m-p/1659655#M74798</link>
      <description>&lt;P&gt;If you want speed, you can get a big bump by not getting the count, but instead just trying to iterate a cursor and immediately returning True if there's rows. Couple that with Dan's isFeatureLayer check and you can do everything in a pretty compact filter operation:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

def has_rows(lyr: arcpy.mp.Layer) -&amp;gt; bool:
    if not lyr.isFeatureLayer:
        return False
    with arcpy.da.SearchCursor(lyr, 'OBJECTID') as cur:
        for _ in cur:
            return True
    return False


def get_field_names(map: arcpy.mp.Map) -&amp;gt; None:
    for lyr in filter(has_rows, map.listLayers()):
        for field in arcpy.ListFields(lyr):
            print(field.name)
            
if __name__ == '__main__':
    aprx = '&amp;lt;project&amp;gt;.aprx'
    m = arcpy.mp.ArcGISProject(aprx).listMaps()[0]
    get_field_names(m)
    &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have broken layers in a map, you can also make sure that the feature layer has a valid datasource:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def has_rows(lyr: arcpy.mp.Layer) -&amp;gt; bool:
    if not lyr.isFeatureLayer or lyr.isBroken:
        return False
    with arcpy.da.SearchCursor(lyr, 'OBJECTID') as cur:
        for _ in cur:
            return True
    return False&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 22 Oct 2025 15:18:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/listfields-after-getcount-throws-error/m-p/1659655#M74798</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-10-22T15:18:52Z</dc:date>
    </item>
    <item>
      <title>Re: ListFields after GetCount throws error</title>
      <link>https://community.esri.com/t5/python-questions/listfields-after-getcount-throws-error/m-p/1659893#M74805</link>
      <description>&lt;P&gt;Hi Trevor&lt;/P&gt;&lt;P&gt;I think you may also want to add a pair of brackets ( ) after &lt;FONT face="courier new,courier"&gt;m.listLayers&lt;/FONT&gt; as &lt;FONT face="courier new,courier"&gt;listLayers()&lt;/FONT&gt; is a method on the Map class:&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/3.4/arcpy/mapping/map-class.htm" target="_blank"&gt;Map—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Oct 2025 11:21:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/listfields-after-getcount-throws-error/m-p/1659893#M74805</guid>
      <dc:creator>EdMorris</dc:creator>
      <dc:date>2025-10-23T11:21:05Z</dc:date>
    </item>
    <item>
      <title>Re: ListFields after GetCount throws error</title>
      <link>https://community.esri.com/t5/python-questions/listfields-after-getcount-throws-error/m-p/1676453#M75042</link>
      <description>&lt;P&gt;yes, i messed up the copy and paste.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jan 2026 19:08:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/listfields-after-getcount-throws-error/m-p/1676453#M75042</guid>
      <dc:creator>TrevorWeiland</dc:creator>
      <dc:date>2026-01-07T19:08:56Z</dc:date>
    </item>
  </channel>
</rss>

