0

I am needing to be able to run a python script on a folder full of shapefiles to delete multiple fields in the attribute tables. But I want to be able to keep 3 fields (Crop_Type, Acres, and Field_Name). The code included below was taken from another post on here however im unsure how to edit it to keep the three fields that I want to keep along with the required fields that ArcGIS 10.5 needs. I can't get it to run, and I am not sure where to go from here.

Here is my current code:

import arcpy
import os

arcpy.env.workspace = "D:\Big Hill-East Gospel"
fcList = arcpy.ListFeatureClasses()

for fc in fcList:
   desc = arcpy.Describe (fc)
   #shape field name
   shpFld = desc.shapeFieldName
   #oid field
   oidFld = desc.OIDFieldName
   #shapefile name field
   shpFileFld = os.path.splitext (fc) [0]
   #list fields
   delFlds = [f.name for f in arcpy.ListFields (fc)
             if not f.name in [shpFld, oidFld, shpFileFld]]

#delete fields
arcpy.DeleteField_managemnet (fc, delFlds)
  • 1
    if not f.name in [shpFld, oidFld, shpFileFld,'Crop_Type','Acres','Field_Name']] but be careful, python is case sensitive, so a field caled 'acres' isn't 'Acres' and will be deleted.. I suggest to either upper or lower case all the strings. – Michael Stimson Mar 21 at 6:01
  • 1
    Also move last line inside your loop. – FelixIP Mar 21 at 6:05
  • 1
    You can use the required property to keep objectid and shapefield, no need to describe: delFlds = [f.name for f in arcpy.ListFields(fc) if (f.name.upper() not in ['CROP_TYPE', 'ACRES', 'FIELD_NAME'] and not f.required)] – BERA Mar 21 at 6:13
  • Make sure you have a validated backup before doing destructive bulk edits. I usually add a confirmation flag option, and if not confirmed, just print "Wold have deleted foo from bar" – Vince Mar 21 at 10:30
1

Try this way:

import arcpy
import os

arcpy.env.workspace = "D:\Big Hill-East Gospel"  # not good.. \ is an escape char so the path will be d:\ig Hill-East..
arcpy.env.workspace = r"D:\Big Hill-East Gospel" # right, use a raw string (no escaping) or "D:\\Big Hill-East Gospel"
fcList = arcpy.ListFeatureClasses()

for fc in fcList:
   desc = arcpy.Describe (fc)
   #shape field name
   shpFld = desc.shapeFieldName.lower()
   #oid field
   oidFld = desc.OIDFieldName.lower()
   #shapefile name field
   #shpFileFld = os.path.splitext (fc) [0] # not needed for your code
   #list fields
   delFlds = [f.name for f in arcpy.ListFields (fc)
             if not f.name.lower() in [shpFld, oidFld,'crop_type','acres','field_name']]

    #delete fields # needed indentation to be included in the for fc in fcList loop
    arcpy.DeleteField_managemnet (fc, delFlds)

There's a few minor errors that need to be fixed.. firstly the arcpy.env.workspace setting to a string, either use a raw string or double slash (see comment) otherwise you'll end up with a letter missing or an incorrect character.

Also as FelixIP said, move the delete fields into the loop, otherwise you're only doing the actual deletion on the last feature class.

In the loop I have made all the necessary field names lower case with .lower() and am comparing f.name as lower case as well.. either upper or lower works but pick one and use it consistently, python is case sensitive so failing to modify the case can cause horrible side effects later.

To help explain the list comprehension:

delFlds = [f.name for f in arcpy.ListFields (fc)
         if not f.name.lower() in [shpFld, oidFld,'crop_type','acres','field_name']]

Is saying to make a list of all the names of the listed fields that aren't contained in the list [shpFld, oidFld,'crop_type','acres','field_name']

You could write it like this if it makes more sense to you:

delFlds = [] # a new empty list
for f in arcpy.ListFields(fc):
    # check if the name is one of the reserved fields
    if not f.name.lower() in [shpFld, oidFld,'crop_type','acres','field_name']:
        delFlds.append(f.name) # append the name to the list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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