How can I delete a field with a different name in a list of feature classes?

4310
4
Jump to solution
01-17-2015 02:07 PM
GabrielBacca-Cortes
New Contributor II

Hi All -

I am trying to delete a field in list of feature classes that changes name depending upon FC is in. This field in all FC has two things in common:

i) the field name starts with "FID_" in all the FCs,

ii) the field is located third [3rd] in the list of fields, right after the fields "FID" and "Shape".

With this in mind, I've tried a couple of things, as shown below, but they don't seem to work. I appreciate any ideas/suggestions you may have.

thanks !

Gabriel

import arcpy, os

from arcpy import env

env.workspace = r"C:\GIS\Track1\1_Orig\Track_int1"

wildcard = "xy**.shp"

fctype = "POLYLINE"

try:

    fclist =  arcpy.ListFeatureClasses(wildcard, fctype)

    for fc in fclist:

# I'VE TRIED THIS:       

##        desc = arcpy.Describe(fc)

##        # Get a list of field objects from the describe object

##        fields = desc.fields

##

##        for field in fields:

##            if field.name == "FID_**":

##                arcpy.DeleteField_management(fc, "FID_**")

##                break

# AND THIS:

            fieldList =  arcpy.ListFields(fc)

            fieldToDel =  fieldList[3:]

           arcpy.DeleteField_management(fc, fieldToDel)

except:

    print arcpy.GetMessages(0)

    print arcpy.GetMessages(1)   

    print arcpy.GetMessages(2)

0 Kudos
1 Solution

Accepted Solutions
JoshuaCrough
New Contributor III

This should work:

import arcpy, os

from arcpy import env

env.workspace = r"C:\GIS\Track1\1_Orig\Track_int1"

wildcard = "xy**.shp"

fctype = "POLYLINE"

try:

    fclist =  arcpy.ListFeatureClasses(wildcard, fctype)

    for fc in fclist:

        fieldList = arcpy.ListFields(fc)

        for field in fieldList:

            if field.name[:4] == "FID_":

                deleteField = field.name

                arcpy.DeleteField_management(fc, deleteField)

except:

    print arcpy.GetMessages(0)

    print arcpy.GetMessages(1)  

    print arcpy.GetMessages(2)

View solution in original post

0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

the 3rd field is index number 2 ie fieldList[2]  please try this and any other attempts on backups!!!!

0 Kudos
GabrielBacca-Cortes
New Contributor II

Dan -  thanks for your response.

I've tried that but I am still getting the same errors:

ERROR 000622: Failed to execute (DeleteField). Parameters are not valid.

ERROR 000623: Invalid value type for parameter drop_field.

any other ideas ?

thanks

Gabriel

0 Kudos
JoshuaCrough
New Contributor III

This should work:

import arcpy, os

from arcpy import env

env.workspace = r"C:\GIS\Track1\1_Orig\Track_int1"

wildcard = "xy**.shp"

fctype = "POLYLINE"

try:

    fclist =  arcpy.ListFeatureClasses(wildcard, fctype)

    for fc in fclist:

        fieldList = arcpy.ListFields(fc)

        for field in fieldList:

            if field.name[:4] == "FID_":

                deleteField = field.name

                arcpy.DeleteField_management(fc, deleteField)

except:

    print arcpy.GetMessages(0)

    print arcpy.GetMessages(1)  

    print arcpy.GetMessages(2)

0 Kudos
GabrielBacca-Cortes
New Contributor II

Thank you Joshua !  that did the trick...   i am really new to this and during my first readings I had seen that format [:4], but had completely forgotten.  thanks again !

Gabriel

0 Kudos