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(), "")
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked May 2, 2015 at 18:53
3
  • 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. Commented May 2, 2015 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? Commented May 2, 2015 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. Commented May 2, 2015 at 21:57

2 Answers 2

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.

answered May 2, 2015 at 19:19
2
  • 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. Commented May 2, 2015 at 22:14
  • name instead of object Commented May 2, 2015 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())
answered May 3, 2015 at 14:09
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.