This thread is quite old, but I still hope for getting some advice. I found posted codes very useful and applied their variation several times very successfully. I'm very grateful to you guys for sharing it. However, recently I faced with very strange behavior of the code. So, what i'm trying to achieve is to join shapefile with feature class and then delete all unnecessary fields except needed. Both input files has same field names so that after join the output file has fields like this: CID, TYPE, STATUS, X, Y, CID_1, TYPE_1, STATUS_1 etc. This is fine. But deleting unnecessary fields in the output doesn't work and I can't figure out why.
from arcpy import env
try:
env.qualifiedFieldNames = False
inFeature = scratch+"\\SK.shp"
inLayer = "SK_lyr"
joinFeature = current+"\\SK_Point"
joinLayer = "SK_Point_lyr"
inLayerField = "CID"
joinLayerField = "CID"
outFeature = current+"\\SK_Poly"
arcpy.MakeFeatureLayer_management(inFeature, inLayer)
arcpy.MakeFeatureLayer_management(joinFeature, joinLayer)
arcpy.AddJoin_management(inLayer, inLayerField, joinLayer, joinLayerField, "KEEP_ALL")
arcpy.CopyFeatures_management(inLayer, outFeature)
except Exception, e:
import traceback, sys
tb = sys.exc_info()[2]
print "Line %i" % tb.tb_lineno
print e.message
infc = current+"\\SK_Poly"
keep = ["CID", "TYPE", "STATUS", "X", "Y"]
try:
discard = []
for field in [f.name for f in arcpy.ListFields(infc) if f.type <> 'OID' and f.type <> 'Geometry']:
if field not in keep:
discard.append(field)
arcpy.DeleteField_management(infc, discard)
except:
arcpy.GetMessages(2)