2

I'm trying to write a script that allows me to list .mxd files in a folder and then list the layers in each map document, however, I keep getting this error:

Traceback (most recent call last): File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec codeObject in main.dict File "C:\Users\Daimon Nurse\Desktop\DFMPROJECT\Scripts\editmapdocument8.py", line 9, in lyr = arcpy.mapping.ListLayers(file) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\mapping.py", line 1499, in ListLayers result = mixins.MapDocumentMixin(map_document_or_layer).listLayers(wildcard, data_frame) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 822, in listLayers layers = self.layers File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 682, in layers for frame in reversed(self.dataFrames): File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 694, in dataFrames return map(convertArcObjectToPythonObject, self.pageLayout.dataFrames) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 678, in pageLayout return convertArcObjectToPythonObject(self._mxd._arc_object.pageLayout) AttributeError: 'unicode' object has no attribute '_arc_object'

This is the code I am using :

import arcpy
import os

PATH2 = r"C:\Users\Daimon Nurse\Desktop\DFMPROJECT"
arcpy.env.workspace = PATH2
arcpy.env.overwriteOutput=True

for file in arcpy.ListFiles("*.mxd"):
    lyr = arcpy.mapping.ListLayers(file)
    print file    
1
  • 1
    ListLayers says that it requires a MapDocument or Layer object. Could that be the problem? Check out the 2nd reply in this newsgroup thread
    – mkennedy
    Jan 9, 2015 at 23:45

3 Answers 3

5

Try converting "file" to a full pathname, then opening the doc with mapping.MapDocument, like this:

for file in arcpy.ListFiles("*.mxd"):
    filePath = os.path.join(PATH2,file)
    print filePath
    MapDoc = arcpy.mapping.MapDocument(filePath)
    lyrList = arcpy.mapping.ListLayers(MapDoc)
    for Lyr in lyrList:
        print Lyr
2
  • Sorry, i don't know how to format code in this forum. :(
    – ESarow
    Jan 9, 2015 at 23:57
  • That's OK - just select the code block and hit the {} button if you are using a browser. It's a bit harder from the mobile clients but the same principle.
    – PolyGeo
    Jan 10, 2015 at 0:13
1

You are trying to list layers from a string called file that happens to be a map (*.mxd) name.

You need to use arcpy.MapDocument() to make a map document object from that first.

1

For arcpy.mapping.ListLayers to work, its input must be a map object. Map objects are created with arcpy.MapDocument()

Try:

for file in arcpy.ListFiles("*.mxd"):
    mxd = arcpy.MapDocument(file) 
    lyr = arcpy.mapping.ListLayers(mxd)
    print file
1
  • I made the changes, but now its saying invalid MXD filename Jan 9, 2015 at 23:59

Your Answer

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

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