I'm trying to loop through a list of feature classes in a geodatabase, extract the polygons whose ATTRIBUTE field values end with x or d, and then copy features into a new feature classes into a new geodatabase. It works to select the correct attributes in one of the fcs, but then it exports it out as a shapefile titled Ditched_Excavated (the name of my output gdb), and keeps overwriting this for each input fc. I'm stumped... TIA!
#Define new clipped NWI
NWI_clip_fgdb = r'F:\Data\NWI\NWI_Wetlands_State_Clip.gdb'
arcpy.env.workspace = NWI_clip_fgdb
clipped_NWI_fcs = arcpy.ListFeatureClasses()
print (clipped_NWI_fcs)
#Define output gdb
channelized_gdb = r'F:\Data\NWI\Ditched_Excavated.gdb'
# loop throughe each input fc, extract ditched and excavated polygons, write to new fc.
for clipped_NWI_fc in clipped_NWI_fcs:
arcpy.MakeFeatureLayer_management(clipped_NWI_fc, "lyr")
whereclause = "ATTRIBUTE LIKE '%r' Or ATTRIBUTE LIKE '%x'"
selection_type = "NEW_SELECTION"
arcpy.SelectLayerByAttribute_management("Lyr", selection_type, whereclause)
in_nwi_path = os.path.join(NWI_clip_fgdb, clipped_NWI_fc)
out_name = clipped_NWI_fc + "Channelized"
ditched_features_path = os.path.join(out_name , channelized_gdb)
arcpy.CopyFeatures_management("Lyr", ditched_features_path)
Solved! Go to Solution.
Hi Kathleen,
It looks like your 'lyr' was changed to 'Lyr' in your code and your
ditched_features_path = os.path.join(out_name , channelized_gdb)
is backwards, putting your gdb last.
I think you will need to get the name of the fc, using the describe:
fc_desc = arcpy.Describe(clipped_NWI_fc)
out_name = fc_desc.baseName + 'Channelized'
ditched_features_path = os.path.join(channelized_gdb, out_name)
Hi @KathleenHoenke ,
Try changing
# from this
ditched_features_path = os.path.join(out_name, channelized_gdb)
# to this
ditched_features_path = os.path.join(channelized_gdb, out_name)
Hope that helps!
Hi Kathleen,
It looks like your 'lyr' was changed to 'Lyr' in your code and your
ditched_features_path = os.path.join(out_name , channelized_gdb)
is backwards, putting your gdb last.
I think you will need to get the name of the fc, using the describe:
fc_desc = arcpy.Describe(clipped_NWI_fc)
out_name = fc_desc.baseName + 'Channelized'
ditched_features_path = os.path.join(channelized_gdb, out_name)
Thank you! I didn't end up needing the describe, but the other changes fixed it, thank you!
Hi @KathleenHoenke ,
Try changing
# from this
ditched_features_path = os.path.join(out_name, channelized_gdb)
# to this
ditched_features_path = os.path.join(channelized_gdb, out_name)
Hope that helps!
This worked, thank you!