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 
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 9, 2015 at 23:21
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 Commented 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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Jan 9, 2015 at 23:57
2
  • Sorry, i don't know how to format code in this forum. :( Commented 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. Commented 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.

answered Jan 9, 2015 at 23:47
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
answered Jan 9, 2015 at 23:52
1
  • I made the changes, but now its saying invalid MXD filename Commented Jan 9, 2015 at 23:59

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.