[GIS] Listing field name aliases in one shapefile/layer using ArcPy

aliasarcpyfields-attributeslist

I want to use arcpy/python to print out a list of all the field name aliases for a specified shapefile. So far I have been able to hack a script I found to list all the field name aliases associated with every layer file on my map, but I wish for it to reference one specific shapefile. Here's what I have so far:

Code:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    desc = arcpy.Describe(lyr)
    print lyr.name
    fields =  desc.fields
    for field in fields:
        print field.aliasName

The above returns a list of all field alias names for ALL of the layers in the map. I want to return just a list for ONE shapefile.

How would I go about referencing just one shapefile/layer in a map document?

Best Answer

Instead of iterating over the whole list returned by ListLayers, pass in the wildcard parameter using your layer name and just process the first item, e.g.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "layer_name")[0]
desc = arcpy.Describe(lyr)
print lyr.name
fields =  desc.fields
for field in fields:
    print field.aliasName
Related Question