I have many shapefiles. Each shapefile has a specific name which is the same as one of the column of data within the attribute table. For each shapefile, I would like to delete all the fields that don't have the same name as the shapefile name.
For example, shapefile might be named m_1. Within its attribute table, there will be lots of columns, which are named "gid", m_1, m_2, m_3, m_3 etc. I would like to delete all those fields except the column "gid" and "m_1". Below is my code, which doesn't work.
I'm new to programming.
import arcpy
arcpy.env.workspace = "D:\Paper"
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
field_names = []
field_names = [f.name for f in arcpy.ListFields(fcList)]
if field_names <> fcList:
arcpy.DeleteField_management(fc, field_names)
{}
button that enables you to format any highlighted code nicely.gid
if you would like to delete all fields that don't have the same name as the shapefile name ?