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.

I have a very simple python script that opens an mxd from a folder and then loops through each layer, and finds a ObjectID field and changes the alias name.

#setup
import arcpy
from arcpy import env

#Go into folder where mxd's are
env.workspace = r"D:\Temp\Test_Mxd"
#for each mxd
for mxd in arcpy.ListFiles("*.mxd"):
    print "Working on map document: " + mxd
    mapdoc = arcpy.mapping.MapDocument("D:\\Temp\\Test_Mxd\\" + mxd)
    #   for each dataframe
    df = arcpy.mapping.ListDataFrames(mapdoc,"Layers")[0]
    ##layers = arcpy.mapping.ListLayers(mapdoc,"*",df):
    layers = arcpy.mapping.ListLayers(mapdoc,"*",df)

    #   for each layer
    for layer in layers:
        print "Working on layer: " + layer.name
        **fieldList = arcpy.ListFields(layer)**
        for field in fieldList:
            if field.name == "OBJECTID":
        # Set the new alias name
                field.aliasName == "IGNORE::OBJECTID"
    mapdoc.save()
del mxd   

my problem is on the line fieldList = arcpy.ListFields(layer), I get a error in the interpreter (pythonwin) which says that the layer does not exist. Has anyone got any idea on where I am going wrong?

Thanks.

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

The documentation here says that ListFields takes a String parameter. So I would try with the datasource path, rather than a layer object itself. I.e. something like:

if layer.supports("DATASOURCE"):
    fieldList = arcpy.ListFields(layer.dataSource)
share|improve this answer
add comment

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.