is there a better way to test for a field and feature class type??

338
1
04-28-2011 09:30 AM
FrankVignati
Occasional Contributor II
here is some code that runs in an mxd from the tool box, the user chooses the layer to do a search on, the script tests the layer to see if the field to do the search on exists then tests the layer to see if it is from a file geodatabase or an sde database by checking wether the layer is versioned so that the field will have quotation marks around it in the selection query for a file geodatabase feature class
the script works, but is there a better way to test for these things?

## Get the selection layer
SelLayer = arcpy.GetParameterAsText(0)


## Check to see if the selection layer has a FIELD field
try: Afld
except NameError:
   pass
else:
   del Afld
   
for flds in arcpy.ListFields(SelLayer, "FIELD"):
    Afld = True

try: Afld
except NameError:
   arcpy.AddMessage(r"!! There is no FIELD field in %s, please select a layer with an FIELD field !!"%(SelLayer))
   quit()
else:
   del Afld

## set the ALTKEY field based on the layer type
check = arcpy.Describe(SelLayer)
if check.isVersioned:
   FLD = "FIELD"
else:
   FLD = '"FIELD"'
Tags (2)
0 Kudos
1 Reply
LoganPugh
Occasional Contributor III
If all you want to do is put the correct field delimiters on a field name, take a look at the AddFieldDelimiters function.
0 Kudos