Hello, I have the following script:
<SPAN class="keyword token">import</SPAN> arcpy
fileno <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Define the path of the mxd</SPAN>
mxd_file <SPAN class="operator token">=</SPAN> <SPAN class="string token">'o:/plan/subdiv/'</SPAN> <SPAN class="operator token">+</SPAN> fileno <SPAN class="operator token">+</SPAN> <SPAN class="string token">'/'</SPAN> <SPAN class="operator token">+</SPAN> fileno <SPAN class="operator token">+</SPAN> <SPAN class="string token">'-ortho_photo.mxd'</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'fileno: '</SPAN> <SPAN class="operator token">+</SPAN> fileno <SPAN class="operator token">+</SPAN> <SPAN class="string token">' mxd_file: '</SPAN> <SPAN class="operator token">+</SPAN> mxd_file<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_file<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>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">'roads'</SPAN><SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>replaceDataSource<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'X:/GIS/geo_databases/labelling_features.gdb'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'FILEGDB_WORKSPACE'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'roads'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">'railways'</SPAN><SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>replaceDataSource<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'X:/GIS/geo_databases/labelling_features.gdb'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'FILEGDB_WORKSPACE'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'railways'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">'rivers'</SPAN><SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>replaceDataSource<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'X:/GIS/geo_databases/labelling_features.gdb'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'FILEGDB_WORKSPACE'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'rivers'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">'lakes'</SPAN><SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>replaceDataSource<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'X:/GIS/geo_databases/labelling_features.gdb'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'FILEGDB_WORKSPACE'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'lakes'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">'proposed_OUTLINE_PLAN_polygon - OP item Site Polygon'</SPAN><SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>replaceDataSource<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'O:/plan/subdiv/'</SPAN> <SPAN class="operator token">+</SPAN> fileno <SPAN class="operator token">+</SPAN> <SPAN class="string token">'/data_for_item.gdb'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'FILEGDB_WORKSPACE'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'proposed_OUTLINE_PLAN_polygon - OP item Site Polygon'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Remove layer from ortho_photo.mxd</SPAN>
<SPAN class="keyword token">for</SPAN> df <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListDataFrames<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</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="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> df<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">'site_polygon'</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>RemoveLayer<SPAN class="punctuation token">(</SPAN>df<SPAN class="punctuation token">,</SPAN> lyr<SPAN class="punctuation token">)</SPAN>
mxd<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> mxd
Result <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>Result<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>
At line 17 I get the following error when executing:
Traceback (most recent call last):
File "X:\GIS\LUAM\DEV\Scripts\OtherTypes\FixDataSourcesOP_Orthophoto.py", line 17, in <module>
lyr.replaceDataSource('O:/plan/subdiv/LOC2018-0001/data_for_item.gdb','FILEGDB_WORKSPACE','proposed_OUTLINE_PLAN_polygon - OP item Site Polygon')
File "c:\program files (x86)\arcgis\desktop10.2\ArcPy\arcpy\utils.py", line 181, in fn_
return fn(*args, **kw)
File "c:\program files (x86)\arcgis\desktop10.2\ArcPy\arcpy\_mapping.py", line 680, in replaceDataSource
return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))
ValueError: Layer: Unexpected error
The layer 'proposed_OUTLINE_PLAN_polygon - OP item Site Polygon' is part of a feature dataset called 'data', so I don't know if that may have anything to do with it? Although, I did read that if you are using this command with a feature dataset to just reference the geodatabase and it should work fine.
Right now I'm at a bit of a loss regarding this "Unexpected error". Any ideas appreciated. Thanks!