Select to view content in your preferred language

Make feature layer > Select by attributes loop

2947
3
04-30-2012 06:06 AM
LouisEarly
Emerging Contributor
This is my first adventure into Python scripting so excuse my ignorance. I am trying to loop through FC's in a GDB and select by an attribute then copy those feature classes into the GDB. Below is my script so far. Where my question lies is, in the 'SelectLayerByAttribute' process how do I specify the feature layer that was created in the previous step as the FC to do the selecting from? See where my question marks are below, that is where im unsure

import arcpy
from arcpy import env

env.workspace = r"\\Wmsmonfs01\gisprod\SDE Data Files\PERMITTING_ABA.gdb"

# Local Variables
GDB = r"\\Wmsmonfs01\gisprod\SDE Data Files\PERMITTING_ABA.gdb"

# Get a list of all feature classes
fcList = arcpy.ListFeatureClasses()

# Loop through each feature class in the GDB
for fc in fcList:

# Put in error trapping in case an error occurs when running tool
try:

# Make a layer from the feature classes
arcpy.MakeFeatureLayer_management(fc, fc + '_LYR')

# Select features by attribute
arcpy.SelectLayerByAttribute_management("?????????", "NEW_SELECTION", "\"Source\" = 'GAI'")

# Copy selected features to GDB
arcpy.CopyFeatures_management("???????", GDB)
Tags (2)
0 Kudos
3 Replies
LouisEarly
Emerging Contributor
Also I think copy features is the wrong process to use.  What I want is to export the selected features to a new FC and put it into the same GDB
0 Kudos
deleted-user-IR249IovB3CN
Deactivated User
I'm working on a similar problem and could use some guidance. I am trying to create new feature classes based on an attribute selection loop in python from an existing feature class. I can't figure out how to loop through the attribute values to select in a for loop? Perhaps create a list of unique values and then set up an if then statement?

any guidance is much appreciated.
0 Kudos
SolomonPulapkura
Frequent Contributor
This is my first adventure into Python scripting so excuse my ignorance. I am trying to loop through FC's in a GDB and select by an attribute then copy those feature classes into the GDB. Below is my script so far. Where my question lies is, in the 'SelectLayerByAttribute' process how do I specify the feature layer that was created in the previous step as the FC to do the selecting from? See where my question marks are below, that is where im unsure 

import arcpy 
from arcpy import env 

env.workspace = r"\\Wmsmonfs01\gisprod\SDE Data Files\PERMITTING_ABA.gdb" 

# Local Variables 
GDB = r"\\Wmsmonfs01\gisprod\SDE Data Files\PERMITTING_ABA.gdb" 

# Get a list of all feature classes 
fcList = arcpy.ListFeatureClasses() 

# Loop through each feature class in the GDB 
for fc in fcList: 
  
# Put in error trapping in case an error occurs when running tool 
try: 

# Make a layer from the feature classes 
arcpy.MakeFeatureLayer_management(fc, fc + '_LYR') 

# Select features by attribute 
arcpy.SelectLayerByAttribute_management("  ?????????", "NEW_SELECTION", "\"Source\" = 'GAI'") 

# Copy selected features to GDB  
arcpy.CopyFeatures_management("  ???????", GDB)


You would have to do something like this
# Make a layer from the feature classes
fl = arcpy.MakeFeatureLayer_management(fc, fc + '_LYR')

# Select features by attribute
fl_sel = arcpy.SelectLayerByAttribute_management("fl", "NEW_SELECTION", "\"Source\" = 'GAI'")

# Copy selected features to GDB
arcpy.CopyFeatures_management("fl_sel", GDB) 


Yes Copy features may not work. Maybe try Feature Class To Feature Class
0 Kudos