2

I have a python code I'm running in ArcGIS 10.6 that replaces the data source path of all layers in the mxd with a new path. I'm trying to use a wildcard for the current workspace path as many layers in our template mxd have different data source paths.

The part of code in question is this line:

mxd.findAndReplaceWorkspacePaths(r"*", r"P:\JOBS\2018\18-9992\Drawings\GIS")

The r"*" doesn't seem to work as a wildcard.

Here is the full code:

# Importing necessary modules
import arcpy, os

# Setting path to mxd and Data Frame
mxd = arcpy.mapping.MapDocument(r"CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "UTM_12")[0]

#List layers and replace current path with new path
for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
    mxd.findAndReplaceWorkspacePaths(r"*", r"P:\JOBS\2018\18-9992\Drawings\GIS")

#Delete any layers with broken links (no datasource)
for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
    if lyr.isBroken:
        arcpy.mapping.RemoveLayer(df, lyr)

# Saving the mxd
mxd.save()
  • 2
    I think you can use lyr.workspacePath in the first parameter instead of the wildcard. – klewis Mar 14 '18 at 21:33
2

Using lyr.workspacePath worked as long as I used if lyr.isFeatureLayer to avoid groups layers and rasters.

0

Set the layer's workspace path directly instead of using findAndReplaceWorkspacePaths:

for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
    if lyr.isFeatureLayer:
        lyr.workspacePath = r"P:\JOBS\2018\18-9992\Drawings\GIS"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.