I'm trying to write a program to get all the data sources of all layers in a .mxd map document. I am using Python 2.7.10 with ArcGIS 10.4.1. On some .mxd's it seems to work just fine, while on others, calling ListLayers on line 16 simply causes the Python script to suddenly exit. It doesn't say why it stopped, it doesn't give an error. I have tried placing the function where I call ListLayers in a try except statement and it doesn't throw an exception either.
I have put print statements to figure out where the program stops and it always stops right after the call to ListLayers (it seems to never return) Is there a bug with ListLayers or am I using it wrong?
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os<SPAN class="punctuation token">,</SPAN> datetime<SPAN class="punctuation token">,</SPAN> sys
root_directory <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"M:\path_to_mxd\document.mxd"</SPAN>
data_sources <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="comment token"># mapping from resource names to an array of paths to resources with that name</SPAN>
resource_locations <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># a running list of all the drive letters and unc locations used in resources</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">store_data_sources</SPAN><SPAN class="punctuation token">(</SPAN>mxd_path<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">global</SPAN> data_sources<SPAN class="punctuation token">,</SPAN> resource_locations
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"signal 0: before MapDocument"</SPAN><SPAN class="punctuation token">)</SPAN>
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN>mxd_path<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"signal 1: returned from MapDocument"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> mxd<SPAN class="punctuation token">:</SPAN>
layers <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"signal 2: returned from ListLayers"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> layers<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> layers<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"signal 3: looping through layers"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>supports<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"dataSource"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"signal 4: supports dataSource"</SPAN><SPAN class="punctuation token">)</SPAN>
data_path <SPAN class="operator token">=</SPAN> lyr<SPAN class="punctuation token">.</SPAN>dataSource
<SPAN class="comment token"># keep track of resource drive letters and unc locations</SPAN>
splitdrive <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>splitdrive<SPAN class="punctuation token">(</SPAN>data_path<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="punctuation token">(</SPAN>splitdrive<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> splitdrive<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">in</SPAN> resource_locations<SPAN class="punctuation token">:</SPAN>
resource_locations<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>splitdrive<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># keep a list of all resource paths in the mxd</SPAN>
<SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">in</SPAN> data_sources<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> data_path <SPAN class="keyword token">in</SPAN> data_sources<SPAN class="punctuation token">[</SPAN>lyr<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">continue</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
data_sources<SPAN class="punctuation token">[</SPAN>lyr<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>data_path<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
data_sources<SPAN class="punctuation token">[</SPAN>lyr<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>data_path<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">continue</SPAN>
<SPAN class="keyword token">del</SPAN> layers
<SPAN class="keyword token">del</SPAN> mxd
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">dump_str_to_arr_hash</SPAN><SPAN class="punctuation token">(</SPAN>hashmap<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> key <SPAN class="keyword token">in</SPAN> hashmap<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\t"</SPAN> <SPAN class="operator token">+</SPAN> key<SPAN class="punctuation token">)</SPAN>
array <SPAN class="operator token">=</SPAN> hashmap<SPAN class="punctuation token">[</SPAN>key<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> string <SPAN class="keyword token">in</SPAN> array<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\t\t"</SPAN> <SPAN class="operator token">+</SPAN> string<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">str_to_arr_hash_count_items</SPAN><SPAN class="punctuation token">(</SPAN>hashmap<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
count <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN>
<SPAN class="keyword token">for</SPAN> key <SPAN class="keyword token">in</SPAN> hashmap<SPAN class="punctuation token">:</SPAN>
array <SPAN class="operator token">=</SPAN> hashmap<SPAN class="punctuation token">[</SPAN>key<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> item <SPAN class="keyword token">in</SPAN> array<SPAN class="punctuation token">:</SPAN>
count <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">return</SPAN> count
start_time <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>start_time<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Exploring path: '"</SPAN> <SPAN class="operator token">+</SPAN> root_directory <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> root_directory
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
store_data_sources<SPAN class="punctuation token">(</SPAN>root_directory<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"An exception has occurred!"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN>
num_sources <SPAN class="operator token">=</SPAN> str_to_arr_hash_count_items<SPAN class="punctuation token">(</SPAN>data_sources<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># number of unique data sources found</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Number of data sources found: "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>num_sources<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># print the drives that are used by resources</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Drive letters and network paths used by resources:"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> loc <SPAN class="keyword token">in</SPAN> resource_locations<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\t"</SPAN> <SPAN class="operator token">+</SPAN> loc<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Paths to resources:"</SPAN><SPAN class="punctuation token">)</SPAN>
dump_str_to_arr_hash<SPAN class="punctuation token">(</SPAN>data_sources<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN>
end_time <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>end_time<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>