I am a total beginner when it comes to python. I received some help already getting this code started but I hit a wall. I have a directory with 200 folders, each with a geodatabase, feature dataset, and point line and polygon feature classes. Each feature class has a field "Label" in the attribute table. I need to be able to run a search through each feature classes' attribute table and return a specific string value. For example, I need to find all feature classes where diabase is in the label field. The output would give me only the feature classes that contain diabase in the label field. I created a temp feature layer from each feature and did a select by attribute to see if get count returns anything. Here's my code:
import arcpy
arcpy.env.workspace = "X:"
ws = arcpy.ListWorkspaces("*","Folder")
arcpy.env.overwriteOutput = True
tempLayer = r"in_memory\templayer"
field = "Label"
value = "diabase"
where = "%s = '%s'" % (field,value)
dhList = []
for w in ws:
arcpy.env.workspace = w
gdb = arcpy.ListWorkspaces("*", "Access")
for fc in gdb:
arcpy.env.workspace = fc
fcl = arcpy.ListDatasets("*", "Feature")
for fcc in fcl:
arcpy.env.workspace = fcc
fccl = arcpy.ListFeatureClasses("*", "All")
for stil in fccl:
arcpy.MakeFeatureLayer_management(stil, tempLayer, where)
arcpy.SelectLayerByAttribute_management(tempLayer,"SUBSET_SELECTION")
count = str(arcpy.GetCount_management(tempLayer).getOutput)
print count
if count > 0:
dhList.append(stil)
for item in dhList:
print item
The output is giving me a list of all of the feature classes in the directory instead of only the feature classes with diabase. What am I doing wrong?
Thanks for any and all help in advance.
Best Answer
An alternative to using the SelectLayerByAttribute_management tool would be to create a SearchCursor on each featureclass that you are stepping through, in your case represented by the variable "stil". Use a whereclause on the cursor of "[Label] = 'diabase'". For each featureclass where the SearchCursor returns a row that is not equal to "None", this means the attribute was found, and you can add those to your list.
See the code below for an example:
I am pretty basic with my coding skills, but this seems like it should do the trick.
Good luck!
Russell