Select to view content in your preferred language

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

717
2
11-22-2019 09:43 AM
JeffreyCox
Emerging Contributor

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.  

Tags (2)
0 Kudos
2 Replies
MichaelVolz
Esteemed Contributor

Do the problematic mxds cause your python script to crash?

Have you tried to analyze the problematic mxds with MXD Doctor?

JeffreyCox
Emerging Contributor

Running the script does not cause a crash, and there aren't any noticeable problems with the mxds that don't output information- I have not used MXD doctor but will give it a try against some of the mxds and try to include what information I can.

From the initial runs of the tool against the problematic mxds the tool did not find any corrupt objects, my thoughts so far are that there is something about the data connections or structure of the layers within the problem mxds that the arcpy method does not like. The first thing I did was separate the functional from non-functional files into two separate directories-- to try and observe any major differences between them, there is nothing I have found so far. I will continue trying things on my own- will post anything that I find. 

Thank you for the response. 

0 Kudos