deleting fields from a shapefile doesn't work in my python script

2003
2
05-24-2011 01:39 PM
YimeiWang
New Contributor

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

# Set a default workspace
gp.workspace = "C:\\arcgisserver\\gisdata"

# Set a default workspace
gp.toolbox = "management"

KeepList = ['FID', 'Shape', 'AREA', 'PERIMETER', 'PLACENAME', 'INCORP_FLA', 'COUNTY', 'STATE', 'SMARTLABEL', 'JURIS_NA_8', 'TAID']

fields = gp.ListFields("KY_TaxAreas.shp")


for field in fields:
        if field.Name not in KeepList:
                gp.deletefield("KY_TaxAreas.shp", "field.Name")
                print field.Name + " is deleted"



The whole processing seems right, and I got all print message for those fields which I want to delete. However, when I open the shapefile in ArcCatalog, those deleting fields are still there. Any idea?
Tags (2)
0 Kudos
2 Replies
DarrenWiens2
MVP Honored Contributor
Does it work if you remove the quotes from around "field.name"? If it stays in quotes, python tries to delete a field literally called "field.Name", not the variable value.
0 Kudos
YimeiWang
New Contributor
Thank you so much, dkwiens! you fix the problem.

Does it work if you remove the quotes from around "field.name"? If it stays in quotes, python tries to delete a field literally called "field.Name", not the variable value.
0 Kudos