I can't make "findAndReplaceWorkspacePath" work

2831
3
10-21-2010 01:11 PM
EricAubert
New Contributor II
Hello, I'm trying to repath hundreds of layer files but I have not been able to make "findAndReplaceWorkspacePath" work as intended, even using this basic example:

Re-pointing a layer file from C to D drive:
---------------------------------------
lyr = arcpy.mapping.Layer(r"C:\temp\index.lyr")
workPath = lyr.workspacePath  # workPath points to "C:\temp\index.gdb"
workPathNew = workPath.replace("C:\","D:\")
lyr.findAndReplaceWorkspacePath(" ", workPathNew)
lyr.save()  # result: index.lyr still points to C:\temp\index.gdb

I tried,
lyr.findAndReplaceWorkspacePath(workPath, workPathNew)
lyr.save()  # result:  "Unexpected error"

I tried to run the script in the Python window or Pythonwin but the results are the same.

Thanks for any help I can get.

BTW:  lyr.saveACopy("C:\temp\index_new.lyr", "9.3") does not work for me.  Here is the message I get:  "function takes exactly 1 argument (2 given)"

Eric
0 Kudos
3 Replies
JeffBarrette
Esri Regular Contributor
Hello Eric,

I'm not sure if the problem you are having is with your .replace statement.  You are not using the raw (r) format, the back slashes may be causing a problem.

Try:

lyr = arcpy.mapping.Layer(r"C:\temp\index.lyr")
lyr.findAndReplaceWorkspacePath(r"C:\", r"D:\")
lyr.save()
0 Kudos
EricAubert
New Contributor II
Jeff, thanks for the reply.  You are correct, I should have used the "raw" format in my example, my fault. 

After carefully reading the documentation, I realized that by default the "validate" is set to True.  In my case ,the database that I was trying to re-point the layer file did not exist yet.  This syntax now works:

lyr.findAndReplaceWorkspacePath("", workPathNew, False)
or
lyr.findAndReplaceWorkspacePath(workPath, workPathNew, False)

Eric
JeffBarrette
Esri Regular Contributor
Hello Eric,

Based on the testing we've done together, it appears your issue is resolved with SP1.  Several issues have been addressed that are specific to change SDE data sources.

Thanks for your help,
Jeff
0 Kudos