Looping Through FeatureClasses

1296
8
03-25-2014 02:02 AM
benberman
Occasional Contributor
I am trying to build a script that will iterate through each feature class while capturing the last record per the required query. I haven't had a chance to really test this out yet. Just seems like I might be able to streamline the code a bit...any feedback is welcome.

# List People and Feature Classes
people = ['TOM','ADAM','BRIAN','STEVE']
lstfc = arcpy.ListFeatureclasses(workspace)
for fc in lstfc: 
  #Loop through each feature class printing the last record date that was updated by each person
  lstTOM = [row.getvalue('updated_date') for row in arcpy.SearchCursor(fc,"PERSON='TOM')
  print str(people[0]) + "has" + " last" + "updated" + "on" + str(lstTOM[-1])
  lstADAM = [row.getvalue('updated_date') for row in arcpy.SearchCursor(fc,"PERSON='ADAM')
  print str(people[1]) + "has" + " last" + "updated" + "on" + str(lstADAM[-1])
  lstBRIAN = [row.getvalue('updated_date') for row in arcpy.SearchCursor(fc,"PERSON='BRIAN')
  print str(people[2]) + "has" + " last" + "updated" + "on" + str(lstBRIAN[-1])
  lstSTEVE = [row.getvalue('updated_date') for row in arcpy.SearchCursor(fc,"PERSON='STEVE')
  print str(people[3]) + "has" + " last" + "updated" + "on" + str(lstSTEVE[-1])
  
Tags (2)
0 Kudos
8 Replies
JakeSkinner
Esri Esteemed Contributor
Hi BB,

Here is an example on how to do this:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"
env.overwriteOutput = 1

# create list of editors
people = ["TOM", "ADAM", "BRIAN", "STEVE"]

# specify fields to use with search cursor
fields = ["PERSON", "updated_date"]

# loop through each feature class in test.gdb
lstfc = arcpy.ListFeatureclasses("*")
for fc in lstfc:
    # loop through each editor for each feature class
    for editor in people:
        # create an empty list
        list = []
        # create a feature layer for each editor
        arcpy.MakeFeatureLayer_management(fc, "fc_lyr", "PERSON = '" + editor + "'")
        #sort feature layer by updated_date and iterate through each row
        for row in arcpy.da.SearchCursor("fc_lyr", fields, sql_clause=(None, 'ORDER BY updated_date')):
            # append each date to the list
            list.append(row[1])
        print editor + " has last updated on" + str(list[-1]) 
0 Kudos
benberman
Occasional Contributor
Hi BB,

Here is an example on how to do this:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"
env.overwriteOutput = 1

people = ["TOM", "ADAM", "BRIAN", "STEVE"]

fields = ["PERSON", "updated_date"]

lstfc = arcpy.ListFeatureclasses("*")
for fc in lstfc:
    for editor in people:
        list = []
        arcpy.MakeTableView_management(fc, "fc_lyr", "PERSON = '" + editor + "'")
        #sort by updated_date
        for row in arcpy.da.SearchCursor("fc_lyr", fields, sql_clause=(None, 'ORDER BY updated_date')):
            list.append(row[1])
        print editor + " has last updated on" + str(list[-1]) 


Hi Jake, would you kindly comment your code?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I went ahead and added some comments to the code snippet.
0 Kudos
benberman
Occasional Contributor
thanks. Is there an alternative without creating a table view? This script is intended to be run on a dynamic database.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I meant to update that as well.  On the test I performed, I was using a table.  You can use the Make Feature Layer function to do this on a feature class.  Ex:

arcpy.MakeFeatureLayer_management(fc, "fc_lyr", "PERSON = '" + editor + "'")
0 Kudos
benberman
Occasional Contributor
Question also..why is row[1] only being appended to the listed? Should it be something like list.append(row) ?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
With the da cursors, the row variable represents each field specified in the 'fields' list.  So, row[0] represents the 'PERSON' field, row[1] represents the 'updated_date' field.
0 Kudos
benberman
Occasional Contributor
I am attempting the code below to nest and iterate each item in the state list into the SQL query in the SearchCursor. It seems that I am over the list index at some point? The compiler passed the syntax. Please offer feedback.

arcpy.env.workspace = workspace
... lstfc = sorted(arcpy.ListFeatureClasses())
... states = ['AK','AZ','CA','CO','ES','ID','MT','NM','NV','OR','UT','WY']
... for fc in lstfc:
...         for item in states:
...                 lstnew1 = [row.getValue("created_date") for row in arcpy.SearchCursor(fc,"ADMIN_ST='item'","",fields="ADMIN_ST;created_date",sort_fields="created_date D")]
...                 print item+" "+ "last updated" +" "+ fc + " "+ "on" + " " + str(lstnew1[0]) +'\n'+'\n'


Runtime error
Traceback (most recent call last):
  File "<string>", line 7, in <module>
IndexError: list index out of range
0 Kudos