1

What I am trying to do is create a list of shapefiles based on field name values. From there I would like search on 1 field (each shapefile will only contain 1 row) and if the value in that field is greater than X have the code rename the shapefile. What I have so far is:

polyFCs = arcpy.ListFeatureClasses("*_Value.shp", "POLYGON")
for polyf in polyFCs:
    field = "Area"
    with arcpy.da.SearchCursor(polyf, (field)) as cursor:
        for row in cursor:
            row.getValue(field)
            if field > X:
                arcpy.Rename_management(polyf, polyf.replace('.shp','') + "_NewValue")

When I run my code (and I'm pry missing or misunderstanding something) I get a runtime error stating 'A column was specified that does not exist'. I am in 10.1. Thanks for your help.

  • 1
    The cursor takes in a string or a list of field names, try this (polyf, field). – artwork21 Jul 25 '14 at 19:41
2

Your rename won't work because you are replacing the extension with nothing. Use os.path.splitext() to remove the extension and then you can add it back.

polyFCs = arcpy.ListFeatureClasses("*_Value.shp", "POLYGON")

field = "Area" #Don't need to keep redefining this each loop.
field = "SHAPE@AREA" #Or, use this to grab the area of the feature.

for polyf in polyFCs:    
    with arcpy.da.SearchCursor(polyf, field) as cursor:
        for row in cursor:            
            if row[0] > X:
                base,ext = os.path.splitext(polyf)
                arcpy.Rename_management(polyf, "{0}_NewValue{1}".format(base, ext))
  • That seemed to get e a little closer but I still get the runtime error – standard Jul 25 '14 at 20:11
  • Are you running this within ArcMap/Catalogue or are you using an IDLE? – GISKid Jul 25 '14 at 20:14
  • OK the problem was I had a shapefile hiding out that lacked the 'Area' field hence the runtime error. Thanks for all the good tips. The script above works to accomplish this task. – standard Jul 25 '14 at 20:16
  • @standard, it's easier to grab the area from the shape field, with the @ token, yep. – Paul Jul 25 '14 at 20:17
1

Try this for your cursor (as I stated your cursor field parameter needs to be a string or a list and using the da.cursor use row[0] for getting the value):

with arcpy.da.SearchCursor(polyf, field) as cursor:
        for row in cursor:
            if row[0] > X:
                # your rename here
  • Thanks @artwork21, I did try that and am still getting :'RuntimeError: A column was specified that does not exist.' – standard Jul 25 '14 at 20:06
  • 1
    I suspect your current feature class does not have that field, trying doing field name check using arcpy.ListFields and conditional logic while looping through the field list before running your cursor. – artwork21 Jul 25 '14 at 20:13
  • 1
    thanks @artwork21, that was the case. I had a shapefile which didn't get processed for adding and calcing the 'Area' field. – standard Jul 25 '14 at 20:21
-2

Do you have an actual field named 'Area', or are you using a shape_area field?

Assuming you have an actual field 'Area', you need to check the value from row.getValue against X (although where is the value for X coming from?). You're checking the string 'Area'.

Try something like:

for row in cursor:            
    if row.getValue(field) > X:  # I don't use getValue, personally, see below

or

for row in cursor:            
    if row.[0] > X:
  • 3
    Your syntax is slightly off, row.[0] should be row[0] – artwork21 Jul 25 '14 at 19:49
  • 2
    I don't believe the first code you posted works as the da module doesn't support the .getValue() method, per here. – Paul Jul 25 '14 at 19:52
  • Thanks for the suggestions. Yes I do have an 'Area' field which is added to the list of features and calculated for area geometry. So it sounds like I need to cast the values for field ('Area') as an integer? – standard Jul 25 '14 at 19:52
  • @standard, cursors return whatever the data type of the field is. So unless your area field is a string... – Paul Jul 25 '14 at 19:58
  • @Paul the field is a double, here's what I tried most recently and still get the runtime error: polyFCs = arcpy.ListFeatureClasses("*_Value.shp", "POLYGON") for polyf in polyFCs: field = "Area" with arcpy.da.SearchCursor(polyf, field) as cursor: for row in cursor: if row[0] > X: arcpy.Rename_management(polyf, polyf.replace('.shp','') + "_NewValue") – standard Jul 25 '14 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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