4

I need to alter all of the field names through multiple feature classes. I have a pretty good idea of which tools to use but I'm not sure how to loop them through each feature class and each field. This will be completed on a file gdb. I've tried it different ways and usually get a invalid parameter error on the alter field tool.

arcpy.env.workspace = r'C:\Users\Desktop\Geodatabase.gdb\'

arcpy.env.overwriteOutput = True

fcList = arcpy.ListFeatureClasses()

for fc in fcList: 

    fieldList = arcpy.ListFields(fc)

    for field in fieldList:
        arcpy.AlterField_management(fc, field, field.name.upper(), "")
  • When you ran your code I think it would have thrown an error. I think that error message would be useful to include in your question to aid future searches by others with similar requirements. – PolyGeo May 2 '15 at 19:50
  • I was receiving an invalid parameter error. So I believe that I'll need to loop the individual field names instead of the entire list? – James Z May 2 '15 at 20:22
  • Please note that comments are not searchable so I recommend placing the precise error message into your question by using the edit button beneath it. In any event invalid parameter probably reflects that you are using a field object instead of a field name like @mr.adam has answered. – PolyGeo May 2 '15 at 21:57
4

The only issue I can see with your code is that you are passing the field object to the AlterField tool, not the field name. Also, just FYI though it doesn't matter, you don't need to pass empty optional arguments to python functions. Change to:

arcpy.AlterField_management(fc, field.name, field.name.upper())

Alternatively, because tools generally use a field's name as input, it's often more useful to make a list of field names, not field objects. This is easy to do with list comprehension:

fieldList = [f.name for f in arcpy.ListFields(fc)]
for field in fieldList:
    arcpy.AlterField_management(fc, field, field.upper())

A little more info: This should help you understand what I mean when I say "field object". When you use ListFields, the list you get is full of fields as explained in that documentation. Each of those field objects has a number of properties, which you access by referencing them like this: field.propertyname. The list comprehension above could be interpreted like this: "This new list will contain the .name property of all the objects (f in this case) that are returned by the ListFields function," if that makes any sense. Or, a longer code version of same the operation could look like this:

fieldList = []                 #make an empty list
for f in arcpy.ListFields(fc): #iterate through all of the field objects
    fieldList.append(f.name)   #append the name property of each object to the list

Finally, please look at @DWynne's answer.

  • How would I carry the object instead of name? I figured the .name. works for that portion but I've had bit ways error out. – James Z May 2 '15 at 22:14
  • name instead of object – James Z May 2 '15 at 22:20
4

In addition to updating to use field.name, you have a second issue. Just uppercasing the field name isn't going to work either. The tool will finish but will not update the field because the database isn't going to see the difference between MyField and MYFIELD, and your field name will remain as it was.

If you really just need to just uppercase the fieldname, you'll have to change it to something different first as an intermediate step.

fieldList = [f.name for f in arcpy.ListFields(fc)]
for field in fieldList:
    arcpy.AlterField_management(fc, field, 'tempfieldname')
    arcpy.AlterField_management(fc, 'tempfieldname', field.upper())

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.