Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have 273 features in the feature class and i want to select them one by one and use the statement in the following code.

At the line:

*arcpy.SelectLayerByAttribute_management (fc1,"NEW_SELECTION","OBJECTID=79")*, 

The feature whose object id is 79 one of the features. When i use this code, I do it for only that feature. I want to apply the statement to all features one by one with a loop. I try to do it, but i am new for programming with python.

import arcpy
from arcpy import env
env.workspace = "D:\pomme de terre 2\pomme de terre2.gdb"
env.overwriteOutput = 1

fc1 = "Flowlin24K_sl_Uns"
fc2 = "New_Poyline"

arcpy.SelectLayerByAttribute_management (fc1,"NEW_SELECTION","OBJECTID=79")
arcpy.SelectLayerByAttribute_management (fc1,"REMOVE_FROM_SELECTION","StreamLevel>6")
arcpy.SelectLayerByAttribute_management (fc1,"CLEAR_SELECTION")

coordList = []

arcpy.FeatureVerticesToPoints_management("hidden", "vertices", "MID")
rows = arcpy.SearchCursor("vertices")

for row in rows:
    X = row.getValue("POINT_X")
    Y = row.getValue("POINT_Y")
    coordList.append([X, Y])

del row, rows


point = arcpy.Point()
array = arcpy.Array()

for feature in coordList:
    point.X = feature[0]
    point.Y = feature[1]

polyline = arcpy.Polyline(array)

arcpy.CopyFeatures_management(polyline, fc2)
share|improve this question
Can you elaborate a little? I can't see just what you want to do? – Hairy Feb 23 '12 at 10:58

You can pull this off, but it's my understanding that you can't run selections on feature classes, only on feature LAYERS.

From ESRI: The input must be a feature layer or a table view. The input cannot be a feature class or table. ESRI SelectLayerByAttributes Help files

Your code requires a arcpy.MakeFeatureLayer_management() ESRI MakeFeatureLayer Help files Remember to delete the feature layer at the end of the loop

Since you've already got an understanding of search cursors, you could just use the cursor to identify the OID for each loop -- this would eliminate the possiblity of your index being out of range. Each time you loop through your code, change your where clause to the current row's OID.

If you're just getting into Python for ArcMap I HIGHLY recommend checking out Penn State's Online GIS Master's Program -- It's open courseware through the MIT Creative commons license.

share|improve this answer

did you try looping from start point sth like this:

for a in range(0,274):
    obj = "OBJECTID=%s" % (a)
    arcpy.SelectLayerByAttribute_management (fc1,"NEW_SELECTION",obj)
    arcpy.SelectLayerByAttribute_management (fc1,"REMOVE_FROM_SELECTION","StreamLevel>6")
    arcpy.SelectLayerByAttribute_management (fc1,"CLEAR_SELECTION")


it should solve your problem...

share|improve this answer
it has a problem at the line 'obj = "OBJECTID=%s" % (a)' The problem may be the identification of OBJECTID before. What do you think about this? – syd Feb 23 '12 at 11:57
@Aragon N I think your index would cause an overflow. There are only has 273 features. OIDs begin at 0 if I am not mistaken. Therefore, your range should just be (0, 273) as the last feature would have an index of 272. – R.K. Feb 23 '12 at 15:03
You could also use GetCount_management to get the true feature count, then use that for the range. – Chad Cooper Feb 23 '12 at 16:07

Have you looked at the Iterate Feature Selection using ModelBuilder? Once you have your model working you can export out the code to python.

share|improve this answer
Model iterators are not exported to Python. "If your model used an iterator, the iteration logic will not be exported and will have to be replaced with the looping statements in Python." From Exporting a model – blah238 Mar 25 '12 at 0:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.