Why AddField script always close the application?

349
2
Jump to solution
03-31-2022 12:50 AM
TessaKhairaniErmanto
New Contributor II

Dear All,

I've Tried to Add Field using arcpy library but the application always close when I run the code, is there any ideas about this situation?

Thank you

Here is my code:

import arcpy, os, glob, pythonaddins

mxd = arcpy.mapping.MapDocument("Current")
mxd.author = "Me"
arcpy.env.workspace = "CURRENT"
mxd.save()

df = arcpy.mapping.ListDataFrames(mxd)[0]
for fc in df:
    arcpy.AddField_management(fc,"Name","TEXT",field_length=50  )
1 Solution

Accepted Solutions
KimGarbade
Occasional Contributor III

After you save your mxd try getting a reference to the data frame and then the layers in the frame and looping through those:

df = arcpy.mapping.ListDataFrames(mxd,"YourDataFrameNameHere")[0]
for fc in arcpy.mapping.ListLayers(mxd, data_frame=df):
    arcpy.AddField_management(fc,"Name","TEXT",field_length=50)

This code is going to add the attribute "Name" to every feature class in the data frame.

View solution in original post

2 Replies
KimGarbade
Occasional Contributor III

After you save your mxd try getting a reference to the data frame and then the layers in the frame and looping through those:

df = arcpy.mapping.ListDataFrames(mxd,"YourDataFrameNameHere")[0]
for fc in arcpy.mapping.ListLayers(mxd, data_frame=df):
    arcpy.AddField_management(fc,"Name","TEXT",field_length=50)

This code is going to add the attribute "Name" to every feature class in the data frame.

TessaKhairaniErmanto
New Contributor II

Dear Kim,

Yes, That's true, after I am looking back at my ArcMap version, I am currently using an old version, and that script will work for at least 10.6 version, Thanks 🙂

0 Kudos