Problems with arcpy findAndReplaceWorkspacePath?

703
3
Jump to solution
11-11-2020 11:26 PM
JohannesBierer
Regular Contributor

Hello,

I can't find and replace a workspace path with the following script:

# -*- coding: cp1252 -*-
import arcpy, os

arcpy.env.overwriteOutput = True

mxd=arcpy.mapping.MapDocument(r"C:\temp_replaceDatasource\Detail.mxd")

class ReplaceDataSource(object):

    def __init__(self):

        self.mxd = mxd
        
    def DataSourcesReplace(self):
        
        for lyr in arcpy.mapping.ListLayers(self.mxd, ""):
            
            if lyr.isServiceLayer == 1:

                print lyr.name + " is a WMS Layer"      

            else:
                
                print lyr.name + " X " + lyr.dataSource

                newDataSource = lyr.dataSource.replace("R:\\", "\\\\{}\\{}\\".format("Client", "R$"))
                
                print newDataSource

                lyr.findAndReplaceWorkspacePath(lyr.dataSource, newDataSource)

                print lyr.dataSource
            
        mxd.saveACopy(r"C:\temp_replaceDatasource\Detail_WIBAS.mxd")
       
                         
if __name__ == '__main__':

    MyClass = ReplaceDataSource()
    MyClass.DataSourcesReplace()

 

The print statement looks ok but the path is not replaced - nothing happens with the path? Any ideas why?

output of the print statement

Maßnahmenflaeche X R:\SG-A\Karto\Bierer2020\Wiedemann_Erlen_Metten_Gründelbachniederung_Auftragskarte\Bearbeitung.gdb\data\Massnahme
\\Client\R$\SG-A\Karto\Bierer2020\Wiedemann_Erlen_Metten_Gründelbachniederung_Auftragskarte\Bearbeitung.gdb\data\Massnahme
R:\SG-A\Karto\Bierer2020\Wiedemann_Erlen_Metten_Gründelbachniederung_Auftragskarte\Bearbeitung.gdb\data\Massnahme

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

You are using arcmap 10.4.... there are lots of bugs noted on the tech support site, that you may want to look at  for example

Bug BUG-000096779 

Problem: Attempts to find and replace multiple data sources from enterprise geodatabase connections ... 

but there are more

findAndReplaceWorkspacePath issues ... sort newest to oldest 


... sort of retired...

View solution in original post

Tags (1)
3 Replies
DavidPike
MVP Frequent Contributor

Maybe lyr.save()

JohannesBierer
Regular Contributor

Thank you David,

unfortunately I get an error message if I insert lyr.save() after the findAndReplace ... :

line 35, in DataSourcesReplace
lyr.save()
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\arcobjects\mixins.py", line 416, in save
assert hasattr(self, '_lyr'), gp.getIDMessage(89003, "No filename set.")
AssertionError: No filename set.

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

You are using arcmap 10.4.... there are lots of bugs noted on the tech support site, that you may want to look at  for example

Bug BUG-000096779 

Problem: Attempts to find and replace multiple data sources from enterprise geodatabase connections ... 

but there are more

findAndReplaceWorkspacePath issues ... sort newest to oldest 


... sort of retired...
Tags (1)