0

I would like to know the path of a feature class that is in my TOC in ArcMap, how can this be done with ArcPy?

3

If you set the mxd_path variable to the file path of your map document the following code will return the name of all feature layers in the mxd along with the corresponding data source's file path. If you want to return only a specific feature layer/class set the layer_name variable to what appears for that layer in the ArcMap table of contents.

import arcpy
from arcpy import mapping

mxd_path = 'path/to/your/mxd/here/name_of_mxd.mxd'
layer_name = '*'

mxd = mapping.MapDocument(mxd_path)
for lyr in mapping.ListLayers(mxd, layer_name):
    if lyr.isFeatureLayer:
        print 'Layer Name: {0}'.format(lyr.name)
        print 'Layer Path: {0}\n'.format(lyr.dataSource)
3

This will print all of the layers within a mxd if you copy your mxd path to mapPath.

This shows other methods that can be called on a layer object:
http://resources.arcgis.com/en/help/main/10.1/index.html#//00s300000008000000

import arcpy

def printLayerDataSource(mxd):

    lyrList = arcpy.mapping.ListLayers(mxd)   #returns list of type layer, contained within map mxd

    for layer in lyrList:  #goes through all of the layers within a given mxd
        try:

            layersource = layer.dataSource
            print(layersource)

        except:
            print("error")

def main():
    mapPath = "pathToMXD"
    mxdObject = arcpy.mapping.MapDocument(mapPath)
    printLayerDataSource(mxdObject)

main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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