Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

In my script, I have to create field in 15 layers. The name that I wold like to set in this new field is the same of the file's name. But all attempts that I did were worng.

My script:

proj=arcpy.mapping.MapDocument("C:\\analise_mestrado\\analise_215_070.mxd")
fclist= arcpy.mapping.ListLayers(proj)
[<map layer u'20060902_215_070'>, <map layer u'20080721_215_070'>, <map layer u'20090622_215_070'>, <map layer u'20100609_215_070'>, <map layer u'20110527_215_070'>, <map layer u'20010616_215_070'>, <map layer u'20000731_215_070'>, <map layer u'19860911_215_070'>, <map layer u'19870509_215_070'>, <map layer u'19870525_215_070'>, <map layer u'19880628_215_070'>, <map layer u'19930525_215_070'>, <map layer u'19940816_215_070'>, <map layer u'19941104_215_070'>, <map layer u'19850722_215_070'>, <map layer u'19970605_215_070'>, <map layer u'19730726_231_070'>, <map layer u'19960618_215_070'>, <map layer u'20110714_215_070'>]

Error for first attempt

for fc in fclist:
    arcpy.AddField_management(fc,fc,"DOUBLE",12,2)
Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000840: The value is not a String.

Other error, for second attempt

for fc in fclist:
    arcpy.AddField_management(fc,str(fc),"DOUBLE",12,2)
Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000313: The length of Field Name must not be larger than 10  

Other error

for fc in fclist:
    arcpy.AddField_management(fc,str(fc[:6]),"DOUBLE",12,2)
Runtime error <type 'exceptions.TypeError'>: 'Layer' object is unsubscriptable

Is there anyone who can help me?

share|improve this question
    
Can you show all your code? What error do you get? The logic you have outlined above works fine for me in 10.2. –  Alex Tereshenkov Nov 28 '13 at 16:15
1  
Works for me in 10.1 too... the fact that you're trying to add the file name to a field type of DOUBLE might be contributing the problem, but as Alex said, it'd be better if you could show more code... –  Jason Miller Nov 28 '13 at 16:41
    
I re edited the ask. I'm sorry but I haven't learned to correct edition of my post yet. Thanks –  dogosousa Nov 28 '13 at 17:30
add comment

1 Answer 1

You are trying to use a Layer object as a name for the field and since it is not a string, you get an error message.

Your option is to use a layer name (as you see it in the TOC):

mxd = arcpy.mapping.MapDocument("current")
list_layers = arcpy.mapping.ListLayers(mxd)
for lyr in list_layers:
    arcpy.AddField_management(lyr,lyr.name,"DOUBLE",12,2)

or to use a datasetName of the layer to figure out the name of the feature class which this layer is based on:

mxd = arcpy.mapping.MapDocument("current")
list_layers = arcpy.mapping.ListLayers(mxd)
for lyr in list_layers:
    arcpy.AddField_management(lyr,lyr.datasetName,"DOUBLE",12,2)

datasetName returns the name of the layer's dataset the way it appears in the workspace, not in the TOC (table of contents). Check the Properties section of the Layer object reference help page (10.0).

EDIT: as you are trying to add a field to a shapefile, it cannot be larger than 10 characters. So, you can trim your lyr.name to take only first 10 characters by using lyr.name[:10]

share|improve this answer
    
Alex, thaks...but: I'm sorry again, but I'm beginner programer. What does "TOC" means? And my problem continue. I delete part of layer's name in order to reduce its size but it hasn't worked. When I insert field with mouse through table nothing of it happened - error because length of field name. mxd=arcpy.mapping.MapDocument("current") list_layers = arcpy.mapping.ListLayers(mxd) for lyr in list_layers: arcpy.AddField_management(lyr,lyr.name,"DOUBLE",12,2) Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000313: The length of Field Name must not be larger than 10 –  dogosousa Nov 28 '13 at 19:31
    
TOC = Table of Contents. You cannot add a field with a length longer than 10 because you probably are trying to add a field to a shapefile which has this limit on size. –  Alex Tereshenkov Nov 28 '13 at 19:31
    
If I put all shape files into geodatabase will solve this problem? –  dogosousa Nov 28 '13 at 19:40
1  
I've edited my answer to take into consideration shapefiles. File geodatabase feature classes don't have this limit (they have 64 characters limit), so yes, this should help you. Use geodatabase wherever possible and use shapefiles when you don't have any other option. –  Alex Tereshenkov Nov 28 '13 at 19:41
    
Before to write again here I have tried to do lyr.name[:10] even so it haven't worked. It returned "Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000310: The Field name must not start with a number". Well, I will try with geodatabase. Just a minute that I will post result here. Thanks a lot for your help. –  dogosousa Nov 28 '13 at 20:08
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.