Im receiving this error for a script that is trying to change selected string within a .shp file path.
Any suggestion on the error.#arcpy
Runtime error
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\_mapping.py", line 697, in findAndReplaceWorkspacePath
return convertArcObjectToPythonObject(self._arc_object.findAndReplaceWorkspacePath(*gp_fixargs((find_workspace_path, replace_workspace_path, validate), True)))
ValueError: Layer: Unexpected error
For the following script.
import arcpy
... mxd = arcpy.mapping.MapDocument(r"H:\GQRA_screening_tool\Arcmap\SwecoTemplate_SI_A3_S2.mxd")
... for lyr in arcpy.mapping.ListLayers(mxd):
... if lyr.supports("DATASOURCE"):
... if r"H:\GQRA_screening_tool\Shapefiles".lower() in lyr.dataSource.lower():
... lyr.findAndReplaceWorkspacePath(r"S1", r"S2")
... mxd.saveACopy(r"H:\GQRA_screening_tool\Arcmap\SwecoTemplate_SI_A3_S2.mxd")
... del mxd
(r"S1", r"S2") where are these defined? the full paths are needed
and I notice you have SI and S1 useage which may be a coincidence
Code formatting would help with indentation and provide line numbers as well
the "S1" and "S2" refer to section of around 200 .shp, im wanting to replace the S1 with S2 on all of them.
so i have it looking to the root folder where all these are stored. H:\GQRA_screening_tool\Shapefiles
import arcpy
... mxd = arcpy.mapping.MapDocument(r"H:\GQRA_screening_tool\Arcmap\SwecoTemplate_SI_A3_S2.mxd")
... for lyr in arcpy.mapping.ListLayers(mxd):
... if lyr.supports("DATASOURCE"):
... if r"H:\GQRA_screening_tool\Shapefiles".lower() in lyr.dataSource.lower():
... lyr.findAndReplaceWorkspacePath(r"S1", r"S2")
... mxd.saveACopy(r"H:\GQRA_screening_tool\Arcmap\SwecoTemplate_SI_A3_S2.mxd")
... del mxd
Here's your code using the formatting Dan refers to. Clearly the 'unexpected error' dosen't give you much to chew on, but you probably should look closely at the values of S1 and S2. Using raw string on a quoted variable seems odd to me, but that's just me. See a suggested approach below...
import arcpy
mxd = arcpy.mapping.MapDocument(r"H:\GQRA_screening_tool\Arcmap\SwecoTemplate_SI_A3_S2.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("DATASOURCE"):
if r"H:\GQRA_screening_tool\Shapefiles".lower() in lyr.dataSource.lower():
lyr.findAndReplaceWorkspacePath(r"S1", r"S2")
mxd.saveACopy(r"H:\GQRA_screening_tool\Arcmap\SwecoTemplate_SI_A3_S2.mxd")
del mxd
S1_var = r'X:\path\to\never\never\land'
S2_var = r'X:\path\to\greatness'
....
lyr.findAndReplaceWorkspacePath(S1_var, S2_var)
Sharing with Python since this is an ArcPy question and not ArcGIS API for Python.