Python Error

482
4
02-12-2019 02:15 AM
AnthonyCorcoran1
New Contributor III

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

Tags (2)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

(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

/blogs/dan_patterson/2016/08/14/script-formatting 

0 Kudos
AnthonyCorcoran1
New Contributor III

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

0 Kudos
JoeBorgione
MVP Emeritus

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) 
That should just about do it....
JoshuaBixby
MVP Esteemed Contributor

Sharing with Python‌ since this is an ArcPy question and not ArcGIS API for Python.

0 Kudos