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?


2 Answers 2


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)

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:

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

            layersource = layer.dataSource


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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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