making the gp.UpdateCursor flexible with the row enumeration object

Discussion created by tburley on Mar 16, 2011
Latest reply on Mar 16, 2011 by tburley
I'm working in ArcGIS 9.3.1. I have a script that works to simply write a value passed in as an argument to a particular feature class attribute field.

My question is: is it possible to make the row enumeration object part of the while statement (see code below) flexible to where it could take on whatever value is assigned to the fieldName variable (argument 3)? As written right now, the row enumeration object is hard-coded to work only with an attribute field named "FILE".

I can't figure out how to do this. Relatively trivial, but it's bugging me....

def popfield():
  """This script will loop through a specified field in a
  specified feature class in a specified workspace
  and populate it with a specified value
  Example usage from cmd line:
  auto_populate_field.py <workspace> <feature class> <field name> 
  <value to be written to null string field>"""
    #for 9.2 and above
    import arcgisscripting
    gp = arcgisscripting.create(9.3)
    #for 9.0/9.1
    import win32com.client
    gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
    gp.AddMessage("\n" + "Using ArcMap 9.0/9.1 or above with \
      win32com.clientDispatch..." + "\n")
  gp.Workspace = sys.argv[1]
  Workspace = str(gp.Workspace)
  Workspace = string.replace(Workspace, "\\", "/")
  featureClass = sys.argv[2]

  fieldName = str(sys.argv[3])

  fieldVal = str(sys.argv[4])
  clause = '[' + fieldName + '] IS NULL'
  #use an update cursor to write the argument string value to the
    #specified field
    rows = gp.UpdateCursor(Workspace + "\b" + featureClass, clause)
    row = rows.Next()
    while row:
      row.FILE = fieldVal
      row = rows.Next()
   #Error Handling   
  except arcgisscripting.ExecuteError:
    print gp.GetMessages(2)
  except Exception, ErrorDesc:
    print ErrorDesc.message
  del row
  del rows

  print "DONE"

if __name__ == "__main__":