Select to view content in your preferred language

AddField Failed to Execute???

5406
11
04-30-2013 12:59 PM
MichelleCouden1
Deactivated User
I am trying to join 2 fields in one layer which is a shapefile and join 2 fields in another layer which is a gdb. I think I have the coding right but it is not wanting to add my join field in my shapefile. It gives me a failed to execute AddField on the first one which is the shapefile. Any hints as to what might be wrong??

import arcpy, traceback
from arcpy import env

mxd = arcpy.mapping.MapDocument("CURRENT")

# Join Fields
fc = 'K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\2011_Counts\2011_Annual_Stations\Annual_Stations_2011.shp'
env.workspace = 'K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\District_Labels_Folder\Abilene_Labels.gdb'

fields_to_join = ['!F2011_TRAF!', '!FLAG!']
arcpy.AddField_management(fc, 'COUNT', 'TEXT')
arcpy.CalculateField_management(fc, 'COUNT', ''.join(fields_to_join), 'PYTHON')

fields_to_join = ['!TextString!', '!TFLAG!']
arcpy.AddField_management(inFeatures, 'TRAFFIC', 'TEXT')
arcpy.CalculateField-management(inFeatures, 'TRAFFIC', ''.join(fields_to_join), 'PYTHON')

lstLayers = arcpy.mapping.ListLayers(mxd)

Tags (2)
0 Kudos
11 Replies
ChrisSnyder
Honored Contributor
I think the problem lies in how you are defining your paths (Python is very picky about this).

You are using:

env.workspace = 'K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\District_Labels_Folder\Abilene_Labels.gdb'

which should be rewritten to be either one of the following:

1. env.workspace = r'K:\TASS\_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\District_Labels_Folder\Abilene_Labels.gdb'
2. env.workspace = 'K:\\TASS\4_MAPPING_DATA_SUPPORT\\Traffic_Mapping\\Traffic_Count_Data\\District_Labels_Folder\\Abilene_Labels.gdb'
3. env.workspace = 'K:/TASS/4_MAPPING_DATA_SUPPORT/Traffic_Mapping/Traffic_Count_Data/District_Labels_Folder/Abilene_Labels.gdb'

This is a common issue! So basicall these are all okay:

1. path = r"C:\temp\test.shp"
2. path = "C:\\temp\\test.shp"
3. path = "C:/temp/test.shp"

But this is NOT okay:

path = "C:\temp\test.shp"

By itself, the "\" charater denotes a "new line", not a path delimter.
0 Kudos
MichelleCouden1
Deactivated User
Duh!! Oh my God, How stupid I forgot the r!! The whole program works now. Thanks Chris, for your patience, knowledge and help. I really appreciate your time. Thanks again!!!
0 Kudos