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.
This code works but unnecessarily (for my purposes) loops through all fields when the Layer is empty (slow):
aprx = fullPathToProProject.aprx
m = (arcpy.mp.ArcGISProject(aprx)).listMaps()[0]
for lyr in m.listLayers:
for field in arcpy.ListFields(lyr):
print(field.name)
This skips Layers with no data (much faster) but fails if the Layer actually has data:
aprx = fullPathToProProject.aprx
m = (arcpy.mp.ArcGISProject(aprx)).listMaps()[0]
for lyr in m.listLayers:
if int(arcpy.management.GetCount(lyr).getOutput(0)) > 0:
for field in arcpy.ListFields(lyr):
print(field.name)Error Message:
Traceback (most recent call last):
File "<fullPythonFilePath>.py", line 5, in <module>
for field in arcpy.ListFields(lyr):
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py", line 1228, in ListFields
return gp.listFields(dataset, wild_card, field_type)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 378, in listFields
self._gp.ListFields(*gp_fixargs(args, True)))
OSError: "<layerName>" does not exist
Any suggestions on testing for data and then listing fields?