Add Functionality to Delete a Field (if it exists)

682
2
01-04-2019 01:52 PM
Status: Open
Labels (1)
DevonBurton3
New Contributor II

Currently with ArcPro the delete field tool in data management will generate a warning if a field doesn't exist in the input and error out the task. Now this might be a bug, as I don't think warnings are supposed to stop an iterating script dead in its tracks, but what if you made the code like so:

inFC = arcpy.GetParameterAsText(0)

ListOfInputFields = arcpy.GetParameterAsText(1)

ListOfInputFields = ListOfInputFields.split(";")

for delField in ListOfInputFields: 

    fldLst = arcpy.ListFields(inFC) 

    fldLst2 = []

    for fld in fldLst:

        fldLst2.append(fld.baseName)

    if delField is in fldLst:

        arcpy.DeleteField_management(inputFC,delField)

    else:

        arcpy.AddMessage(deleteField + " was not found in " + inputFC)

        continue

Anyways just an idea. It's probably not the best way of writing out the code but maybe you get the point?

2 Comments
KoryKramer

It seems like the request would be specific to scripting?  But you can already write checks like this into a script...

So is the request to bake an 'if exists' check right into the Delete Field—Data Management toolbox | ArcGIS Desktop tool?

DevonBurton3

Yes, as a parameter. But I can also get that it's better to just write that check into my own scripts at the same time. It'd likely cause confusion though as an optional parameter. Not a good enough reason to change arcpy.DeleteField_management, I suppose.