Replace parts of a data source string in Layer files?

4181
6
Jump to solution
02-09-2015 11:37 PM
JohannesBierer
Occasional Contributor III

Why does this code replace only things like c:\temp\data through c:\tempXXX\data and not the beginning of c:\temp\data\1\2\3 ?

import arcpy, os
# Pfade definieren
folderPath = r"R:\SG-C\8850.20-6 FFH-Gebiete\04 MaP\7218-341_Calwer_Heckengaeu"
oldPath = r"R:\natura 2000\8847.04 MaP\7218-341_Calwer_Heckengäu"
# mxd definieren
path = r"R:\SG-C\8850.20-6 FFH-Gebiete\04 MaP\7218-341_Calwer_Heckengaeu\7218341.mxd"
mxd = arcpy.mapping.MapDocument(path)  
for df in arcpy.mapping.ListDataFrames(mxd):  
    for lyr in arcpy.mapping.ListLayers(mxd): 
        print (lyr)
        lyr.findAndReplaceWorkspacePath(oldPath, folderPath, "")
arcpy.RefreshTOC()  
arcpy.RefreshActiveView()  
mxd.save
del mxd
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

I see the oldPath has "ä" in the name. Python normally doesn't like that and other functions may not work well with these type of characters (for instance working with the raster calculator). Personally I would reconsider if the path should be named like this (also spaces, points, dashes, etc will be a sources or problems when working with rasters calculations).

oldPath = r"R:\natura 2000\8847.04 MaP\7218-341_Calwer_Heckengäu"

You may have to use the next line as first line in your script:

# -*- coding: utf-8 -*-

If that doesn't work, maybe you should enclose your script by:

import sys
reload(sys)
# read system settings and set utf8
def_enc = sys.getdefaultencoding()
sys.setdefaultencoding('utf8')

# script logic goes here ...

# restore settings
sys.setdefaultencoding(def_enc)

But my recommendation is to reconsider the folder names

View solution in original post

6 Replies
Zeke
by
Regular Contributor III

I think you may want ListLayers(df), not ListLayers(mxd).

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You can't pass ListLayers only a dataframe object, it will fail.  At a minimum, and map document or layer needs to be passed.  That said, the code still has an issue.

Zeke
by
Regular Contributor III

Yes, that's correct, my bad. ListLayers(mxd, "", df) would be more correct.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am not understanding your predicament.  Could you post a screenshot or some specific examples of what you would like to see and what you are actually seeing?

Regarding your code, you don't have to loop over the dataframes if you aren't going to pass them to ListLayers.  Calling ListLayers without a dataframe object will list the layers in all of the dataframes.

0 Kudos
JohannesBierer
Occasional Contributor III

Ok, the code was working to replace c:\temp\data through c:\tempXXX\data. But not if there are more subfolders like c:\tempYYY\datax\dataY\dataZ it failed?

Have tested it with only listlayer. Somehow it steps out. After printing Projektgewaesser ..

ReplacePath.PNG

File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 695, in findAndReplaceWorkspacePath

    return convertArcObjectToPythonObject(self._arc_object.findAndReplaceWorkspacePath(*gp_fixargs((find_workspace_path, replace_workspace_path, validate), True)))

ValueError: Layer: Unexpected error

0 Kudos
XanderBakker
Esri Esteemed Contributor

I see the oldPath has "ä" in the name. Python normally doesn't like that and other functions may not work well with these type of characters (for instance working with the raster calculator). Personally I would reconsider if the path should be named like this (also spaces, points, dashes, etc will be a sources or problems when working with rasters calculations).

oldPath = r"R:\natura 2000\8847.04 MaP\7218-341_Calwer_Heckengäu"

You may have to use the next line as first line in your script:

# -*- coding: utf-8 -*-

If that doesn't work, maybe you should enclose your script by:

import sys
reload(sys)
# read system settings and set utf8
def_enc = sys.getdefaultencoding()
sys.setdefaultencoding('utf8')

# script logic goes here ...

# restore settings
sys.setdefaultencoding(def_enc)

But my recommendation is to reconsider the folder names