AnsweredAssumed Answered

arcpy python method: arcpy.mapping.MapDocument() working for some mxds but not others

Question asked by jjcox1 on Nov 22, 2019
Latest reply on Nov 22, 2019 by jjcox1

I have put together a python script which iterates through a directory of mxd files and outputs their paths, layers, fields, and descriptive information to a csv file. When I run the script, it successfully outputs the desired information for about half of the mxds in the directory, but does not output anything for the others.

 

After breaking the code apart and running it, I can confirm that the paths to these 'broken' mxds are being added to my initial lists with the others, but for some reason the arcpy.mapping.MapDocument(mxd)  method is not able to retrieve their information. (I am passing the mxd paths to this method) My Main question is: what are some potential reasons as to why this method is working for some mxds but not others?

The problem might also lie within the arcpy.mapping.ListLayers(mxd) method. 

 

here is the section of the script where this is happening: (ignore the indentation..the script runs but formatting is lost when I copy it over) 

 

#Iterate through the list of ArcMap Documents...
for mxdpath in mxd_list:
#mxdname = os.path.split(mxdpath)[1]
try:
mxd = arcpy.mapping.MapDocument(mxdpath)
#Iterate through the ArcMap Document layers...
fields =[]
layers=[]

 

for layer in arcpy.mapping.ListLayers(mxd):
layers.append(layer)
fList = arcpy.Describe(layer).fields
for field in fList:
fields.append( "Field = " + field.baseName)
layerattributes = [mxdpath,' ', layer.longName,' ', layer.dataSource,' ',fields]
#Write the attributes to the csv file...
writer.writerow(layerattributes)

except:

arcpy.AddMessage("EXCEPTION: {0}".format(mxdpath))
del mxd

 

there are exceptions thrown when the script encounters the mxds in question. 

through visual studio the only information I can find is:

 

<entry>
<record>728</record>
<time>2019/11/21 22:40:32.740</time>
<type>Error</type>
<source>Editor or Editor Extension</source>
<description>System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.&#x000D;&#x000A;Parameter name: index&#x000D;&#x000A; at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)&#x000D;&#x000A; at Microsoft.NodejsTools.Repl.ReplOutputClassifier.GetClassificationSpans(SnapshotSpan span)&#x000D;&#x000A; at Microsoft.VisualStudio.Text.Classification.Implementation.ClassifierTagger.&lt;GetTags&gt;d__5.MoveNext()&#x000D;&#x000A; at Microsoft.VisualStudio.Text.Tagging.Implementation.TagAggregator`1.&lt;GetTagsForBuffer&gt;d__39.MoveNext()&#x000D;&#x000A;--- End of stack trace from previous location where exception was thrown ---&#x000D;&#x000A; at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)</description>
</entry>

 

I'm not sure how to interpret this, as there is nothing that I can discern to differentiate between the mxds which are working and those which aren't. And to be honest I'm not even entirely sure if this error is something related to the mxds or something to do with visual studio. 

 

Any information is appreciated, unfortunately I cannot provide any info regarding the specific mxds as they contain confidential enterprise info, but if anyone has ideas I am happy to provide non specific info (cache or not?, size? etc..)

 

Thank you.  

Outcomes