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?

Michael B
7774 silver badges22 bronze badges
asked Aug 5, 2015 at 17:59
1

2 Answers 2

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)
answered Aug 5, 2015 at 20:02
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()
answered Aug 5, 2015 at 19:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.