Is "findAndReplaceWorkspacePath" unstable or is it me?

Discussion created by eaub490 on Oct 29, 2010
Latest reply on Oct 5, 2012 by jt1qc
Ok, after hours trying to figure out what's going on with the "findAndReplaceWorkspacePath" I'm just giving up and reaching out for help. So far, I am thinking that this method provides completely unexpected/unstable/unreliable results. But before I get to far with my frustrations, I need somebody to review this code and let me know what I am doing wrong. The problem is that the workspace path of the layer files don't get re-pointed to the new SDE connection file, whether I use validate = True or False. When set to True the script crashes miserably, even though the connection file/parameters is valid, so it this example I set it to False, which you would expect to work, but it does not! This scripts loops through a root folder, recursively finds all the ".lyr" file under the root and re-points the workspacePath location. It can be executed in PythonWin/Idle or the ArcMap Python window, the results are the same i.e. unexpected/unstable/unreliable i.e. the layers don't get re-pointed.

import arcpy, sys, os, traceback, time, sys, random, string
root = r"D:\eaub490\repath_layers\DNR Map Layers\Forest Management"

for path, folders, fileList in os.walk(root):
 print "*************\nFolder: " + path + "\n*************"
 for f in fileList:
  print f
  if f.endswith(".lyr"):
   lyr = arcpy.mapping.Layer(path + "\\" + f)
   if lyr.isGroupLayer == False:
    if lyr.supports("WORKSPACEPATH"):  
     if ".sde" in lyr.workspacePath:
      workPath = lyr.workspacePath
      print " * Old:", workPath
      if lyr.isFeatureLayer:
       workPathNew = r"D:\eaub490\repath_layers\sde_connections_read\ropa_v10.sde"  # this is a valid connection file
       lyr.findAndReplaceWorkspacePath("", workPathNew, False)  # when validate = True then it becomes extremely unstable and unpredictable
       workPathNew = r"D:\eaub490\repath_layers\sde_connections_read\raster_v10.sde"
       lyr.findAndReplaceWorkspacePath("", workPathNew, False)
      lyr.visible = False  # turn the visibility off
      lyr.save()  # save to v.10
      if lyr.workspacePath <> workPathNew:  # check because the findAndReplaceWorkspacePath does not always works
       print " * Dang! the findAndReplaceWorkspacePath does not work:", lyr.workspacePath
       print " * New:", lyr.workspacePath
      print " * Not a SDE layer"
     print " * Workspacepath not supported"
    print " * Group layer, cannot be re-pathed"  # as far as I know
   print " * Not a layer file"
print "\nEnd of program\n"