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
-
1ListLayers says that it requires a MapDocument or Layer object. Could that be the problem? Check out the 2nd reply in this newsgroup threadmkennedy– mkennedy2015年01月09日 23:45:29 +00:00Commented Jan 9, 2015 at 23:45
3 Answers 3
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
-
Sorry, i don't know how to format code in this forum. :(ESarow– ESarow2015年01月09日 23:57:41 +00:00Commented 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.2015年01月10日 00:13:42 +00:00Commented Jan 10, 2015 at 0:13
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.
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
-
I made the changes, but now its saying invalid MXD filenameDaimon Nurse– Daimon Nurse2015年01月09日 23:59:51 +00:00Commented Jan 9, 2015 at 23:59
Explore related questions
See similar questions with these tags.