arcmap_将图层名写入NAME字段 ※
import arcpy
fcs=arcpy.GetParameterAsText(0).split(';')
for fc in fcs:
fld_name_list= [i.name for i in arcpy.ListFields(fc)]
try:
if 'NAME' in fld_name_list:
arcpy.DeleteField_management(fc,'NAME')
arcpy.AddField_management(fc,
field_name='NAME',
field_alias='名称',
field_type='Text',
field_length=254
)
fcName=fc.split('\\')[-1].replace('.shp','')
arcpy.CalculateField_management(in_table=fc,field='NAME',expression='"%s"'%fcName, expression_type='VB')
except Exception:
arcpy.AddError(" featureclass {0},failed to add name to field".format(fcName))
continue