-1

I am trying to automate a loop for the Alter Field tool. As the code snippet that comes with the help fits my problem, I used it by changing the workspace path. I also tried passing the path with GetParameterAsText. The script work fine and ends without bugs, but it run very fast (0.04 s). In fact, when I check the names in the fields I see that it does nothing.

Does anybody know what is the problem?

I'm using win 10 and ArcGIS Desktop 10.4.4. The last code snippet is below.

#Import geoprocessing
import arcpy
import os
#Set workspace
arcpy.env.workspace=arcpy.GetParameterAsText(0)
fcList = arcpy.ListFeatureClasses() #get a list of feature classes
for fc in fcList:  #loop through feature classes
    fieldList = arcpy.ListFields(fc)  #get a list of fields for each feature class
    for field in fieldList: #loop through each field
        if field.name.lower() == 'Elevation':  #look for the name Elevation
            arcpy.AlterField_management(fc, field.name, 'EL', 'Elev')

It neither works even if I use the same code of the example 2 of the tool, but with the exact name 'Elevation':

 #Import geoprocessing
import arcpy

#Set workspace
arcpy.env.workspace = r'E:\DICEA_WORK\SURVEY LAB\MUSA\PROCESSING\INPUT\COSMO\Elaborazione_CSK_continuoASICOSMO_ptc9\Geodatabase.gdb'

#Loop through feature classes looking for a field named 'Elevation'
fcList = arcpy.ListFeatureClasses() #get a list of feature classes
for fc in fcList:  #loop through feature classes
    fieldList = arcpy.ListFields(fc)  #get a list of fields for each feature class
    for field in fieldList: #loop through each field
        if field.name == 'Elevation':  #look for the name elev
            arcpy.AlterField_management(fc, field.name, 'ELEVATION', 'Elevation in Metres')

closed as off-topic by PolyGeo Jun 2 '18 at 7:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Welcome to GIS SE. As a new user, please take the Tour. In the future, please take the time to add diagnostic print statements and evaluate your flow-of-control before posting a question. At a minimum, it will improve your question, and it might even lead you to understand why certain control points are never visited. – Vince Jun 2 '18 at 0:02
3

The scripts reads the lower case field name, and compares it to a name having a capital letter... so the condition is never satisfied!

if field.name.lower() == 'elevation'
  • It neigher worked with: field.name == 'Elevation' neither field.name.lower() == 'levation' – user3265206 Jun 1 '18 at 20:41
  • It neither works even if I use the same code of the example 2 of the tool, but with the exact name 'Elevation': – user3265206 Jun 1 '18 at 21:00
  • @user3265206 Is it really called 'Elevation'? Double check that it is not the alias – JGH Jun 1 '18 at 21:28
  • @user3265206 you may also want to close/reopen Arcmap. I found that sometimes the fields are cached and not properly refreshed after some modifications – JGH Jun 1 '18 at 21:32
  • Now it works! I reopened Arcmap, and did other tries changing the new field name by 'ELEV' or 'COTA', and it changed names perfectly. – user3265206 Jun 2 '18 at 5:07
3

This line:

if field.name.lower() == 'Elevation':

Is impossible. .lower() will always return a string of all lower case letters. The string you have given to compare it with has E in it.

Try using:

if field.name.lower() == 'elevation':

Instead.

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