How to process with a list of files using [arcpy.ListFeatureClasses] with script below? Any help from you all very appreciate!
try: fcList = arcpy.ListFeatureClasses("P*","POLYGON") fcList.sort() print fcList fcCount = len(fcList) print fcCount for fcs in fcList: print "Selecting areas < 1ha from file: " + fcs eliminate_file = fcs + "_elim" arcpy.SelectLayerByAttribute_management(fcs,"NEW_SELECTION", expres) arcpy.Eliminate_management(fcs, eliminate_file, "AREA", "", "" ) except: print "Could not create feature layers"
******
This is from:
# Name: Eliminate_Example2.py # Description: Eliminate features based on a selection. # Import system modules import arcpy # Set environment settings arcpy.env.workspace = "C:/data/Portland.gdb/Census" # Set local variables inFeatures = "blockgrp" tempLayer = "blocklayer" expression = '"Area_Sq_Miles" < 0.15' outFeatureClass = "C:/output/output.gdb/eliminate_output" exclusionExpression = '"OBJECTID" = 9' # Execute MakeFeatureLayer arcpy.MakeFeatureLayer_management(inFeatures, tempLayer) # Execute SelectLayerByAttribute to define features to be eliminated arcpy.SelectLayerByAttribute_management(tempLayer, "NEW_SELECTION", expression) # Execute Eliminate arcpy.Eliminate_management(tempLayer, outFeatureClass, "LENGTH", exclusionExpression)
Message was edited by: Dan Patterson I moved this to geoprocessing and used syntax highlighting >> to format the code
Solved! Go to Solution.
if only the last layer in fcList is processed, you have to check the indention of your code: the Eliminate must be inside the For loop
Not:
for fcs in fcList: print "Selecting areas < 1ha from file: " + fcs eliminate_file = fcs + "_elim" arcpy.SelectLayerByAttribute_management(fcs,"NEW_SELECTION", expres) arcpy.Eliminate_management(fcs, eliminate_file, "AREA", "", "" )
But:
for fcs in fcList: print "Selecting areas < 1ha from file: " + fcs eliminate_file = fcs + "_elim" arcpy.SelectLayerByAttribute_management(fcs,"NEW_SELECTION", expres) arcpy.Eliminate_management(fcs, eliminate_file, "AREA", "", "" )
Your code has a few errors in it.
I don't see where you set the workspace or the expression. Lines 9 and 10 have indentation errors.
Here is the help for Eliminate
I have seen the Eliminate Help and copied the ...Example2.py
However, I want to process a list of layers or feature layers and do not knowhow to use Phyton or arcpy correctly.
I try to use this but no success!!!
fcList = arcpy.ListFeatureClasses("P*","POLYGON")
for fc in fcList:
MakeFeatureLayer...
Select...
Eliminate...
So, it process only the last layer in the fcList.
Any idea?
Thank you so much!
Phoukhong
I would like to Eliminate an area smaller than 15000 sq meters in all layers. Example files attached.
Dropbox - ElimSampleFiles.gdb.zip
Thanks,
Phoukhong
if only the last layer in fcList is processed, you have to check the indention of your code: the Eliminate must be inside the For loop
Not:
for fcs in fcList: print "Selecting areas < 1ha from file: " + fcs eliminate_file = fcs + "_elim" arcpy.SelectLayerByAttribute_management(fcs,"NEW_SELECTION", expres) arcpy.Eliminate_management(fcs, eliminate_file, "AREA", "", "" )
But:
for fcs in fcList: print "Selecting areas < 1ha from file: " + fcs eliminate_file = fcs + "_elim" arcpy.SelectLayerByAttribute_management(fcs,"NEW_SELECTION", expres) arcpy.Eliminate_management(fcs, eliminate_file, "AREA", "", "" )