Hi there,
This is an evolving script. I am essentially teaching myself but it's like stumbling around in the dark.
I have a butt-load of mxd's whose layers' datasources are being changed, both in location and in feature class name (for the most part). I managed to write a script that would walk through a directory and list each mxd, layer name, layer datasource path and layer feature class name and write it to a csv. That worked fine.
Now I'm looking to use an altered form of that list to automate the lyr.replaceDataSource() method.
I have got part of the code working, but when it comes to actually changing the datasource it comes up a dud.
Maybe it's my logic. Maybe the code is too simplistic. I dunno.
Now, I have tested it by hardcoding the paths and values in and it works hunky-dory both with single and double quotes. (thanks to the advice from some helpful people like yourself!)
When I try to use the lists I don't get very far. Maybe someone out there can see what I am doing wrong or leaving out? any advice would be great!
Here's what I got:
<SPAN class="comment token"># This script reads a list in csv format and assigns them to variables to be</SPAN>
<SPAN class="comment token"># used in the lyr.replaceDataSource method. It will then be used to replace old paths with</SPAN>
<SPAN class="comment token"># new sources and change each layer's feature class and layer name for each map in the directory.</SPAN>
<SPAN class="comment token"># **This is just a test code for now**</SPAN>
<SPAN class="keyword token">import</SPAN> csv
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="comment token"># set up blank lists for data</SPAN>
oldP<SPAN class="punctuation token">,</SPAN>newF<SPAN class="punctuation token">,</SPAN>newL <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># read data from file and store in lists</SPAN>
<SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"C:\hardcode_to_Path\dsListTEST.csv"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"rb"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> dsList<SPAN class="punctuation token">:</SPAN>
r<SPAN class="operator token">=</SPAN> csv<SPAN class="punctuation token">.</SPAN>reader<SPAN class="punctuation token">(</SPAN>dsList<SPAN class="punctuation token">,</SPAN>delimiter<SPAN class="operator token">=</SPAN><SPAN class="string token">","</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> i<SPAN class="punctuation token">,</SPAN>row <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>r<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> i<SPAN class="operator token">></SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">#skip the header</SPAN>
oldP<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
newF<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
newL<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># replace the existing datasource with the new path, feature class name and layer name.</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>r<SPAN class="string token">"hardcode_to_testMap.mxd"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</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="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">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>dataSource <SPAN class="operator token">==</SPAN> oldP<SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>replaceDataSource<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"Hardcode_to_new_location"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"None"</SPAN><SPAN class="punctuation token">,</SPAN>newF<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">=</SPAN> newL
<SPAN class="keyword token">print</SPAN> lyr<SPAN class="punctuation token">.</SPAN>dataSource
mxd<SPAN class="punctuation token">.</SPAN>saveACopy<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"hardcode_to_Copy_testMap.mxd"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> mxd
dsList<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><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>
The list append works... and so does the mxd.saveACopy.
BUT the important part in between does not do anything. DUD. 