With the following I am evaluating:
- Does the field exist and correct length. Print message
- Does the field exist and incorrect length. Print message
- Does the field not exist. Create field. Print Message
#Create required TRLNAME Field
try:
in_Field = "TRLNAME"
if len(arcpy.ListFields(in_Table,in_Field))>0 and len(in_Field) == 254:
arcpy.AddWarning(in_Field+" exists and is the correct length. Yay!")
elif len(arcpy.ListFields(in_Table,in_Field))>0 and len(in_Field) != 254:
arcpy.AddWarning(in_Field+" exists but is not the correct string length (254).")
else:
arcpy.AddField_management(in_Table, in_Field, "TEXT", "", "", 254, in_Field, "NON_NULLABLE", "REQUIRED", "")
arcpy.AddMessage ("Created the "+in_Field+" field.")
except arcpy.ExecuteError:
msgs = arcpy.GetMessages(2)
arcpy.AddError(msgs)
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
arcpy.AddError(pymsg)
arcpy.AddError(msgs)
If the field does not exist, it correctly creates it. However, if the field exists, regardless of if it's the correct length (254 or any other string length), it always defaults to the "elif" in the block and reports that the field is not the correct length.
Feel like this is a simple syntax error but I've been at it for hours and not seeing it. Help?