简体   繁体   中英

Overwrite attribute table using python

When I try to use python codes to add fields and update the fields using geometry (x and y) to the attribute table of feature class, it gives me this error:

field A already exists
Failed to execute (AddField).

I used

  arcpy.env.overwriteOutput = True

in my codes but seem not to work.

How to overwrite attribute table of feature class? Does this overwrite code also work for the overwriting attribute table? I know that even for geoprocessing the codes don't work well sometimes.

arcpy.env.overwriteOutput will overwrite existing datasets, not existing fields.

You should ckeck if the field exists, and if it does, either:

Delete it and re-add it:

if len(arcpy.ListFields(your_dataset, A)) > 0:
  arcpy.DeleteField_management(your_dataset, A)
  arcpy.AddField_management(your_dataset, A, field_type)

Not add it:

if len(arcpy.ListFields(your_dataset, A)) > 0:
  arcpy.AddMessage("Field A already exists")
else:
  arcpy.AddField_management(your_dataset, A, field_type)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM