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
Solved! Go to Solution.
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
I think you may want ListLayers(df), not ListLayers(mxd).
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.
Yes, that's correct, my bad. ListLayers(mxd, "", df) would be more correct.
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.
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 ..
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
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