I am trying to create a script that makes a copy of the same feature class within the same file GDB by using the "copyfeatures" syntax which works fine. I am then trying to iterate through a list of .mxd's within a certain directory and find any .mxd with the feature class that I made a copy of and replace that feature class with the newer version or the copy of that feature class that I created earlier in the script.

The scripts seems to be running with the syntax below, the .mxd's are being saved and the script runs with no problems. However, the feature class in the .mxd is not updating.

import arcpy, os
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\GISShare\jehmann\TESTING.gdb"

workspace = r"C:\TEST"
outdata_1 = r"C:\GISShare\jehmann\TESTING.gdb\JeffNotes_SHLs"

# Copy New Version of the Feature Classes

    arcpy.CopyFeatures_management('Notes_SHLs', 'JeffNotes_SHLs')
    print "SHLs Feature Class Created Succesfully!"
    print "Failed to Create SHLs Feature Class"

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, topdown=True):
    arcpy.env.workspace = dirpath
    mxds = arcpy.ListFiles('*.mxd')
    for filename in mxds:
        print "------------------------------"
        print filename
        mxd = arcpy.mapping.MapDocument(os.path.join(dirpath, filename))
        for layer in arcpy.mapping.ListLayers(mxd):
            if layer.supports("DATASOURCE"):
                print "{} -> {}".format(layer.name, layer.dataSource)
                if '%Notes_SHLs%' in layer.dataSource:
                    layer.replaceDataSource(layer.dataSource, "FILEGDB_WORKSPACE", outdata_1)


I think it has something to do with how I have the layer.replaceDataSource set up?

  • 1
    Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format.
    – Midavalo
    Commented Mar 30, 2017 at 17:01
  • 1
    Does it print this line print "{} -> {}".format(layer.name, layer.dataSource)?
    – Midavalo
    Commented Mar 30, 2017 at 17:01
  • 1
    Does it actually enter the if '%Notes_SHLs%' in layer.dataSource: block? Add a print inside that block to test
    – Midavalo
    Commented Mar 30, 2017 at 17:02
  • 1
    Please edit your question to include any clarification in response to comments
    – Midavalo
    Commented Mar 30, 2017 at 17:02
  • it does print the "{} -> {}".format(layer.name, layer.dataSource) line and lists all the layers in each .mxd. When I add the print in the if '%Notes_SHLs%' in layer.dataSource: block it does not print anything?
    – jaehmann
    Commented Mar 30, 2017 at 18:48

1 Answer 1


Take the two % out of this line if '%Notes_SHLs%' in layer.dataSource:

If you want to check if a specific string is within a variable, you need to exclude the % wildcard symbols

if 'Notes_SHLs' in layer.dataSource:

In response to your comment below, do a print on that layer.dataSource rather than the if to determine what is actually there:

if layer.supports("DATASOURCE"):
    print layer.dataSource
  • Thanks for the update, that is what I had tried initially and when I run it that way it is returning an "Unexpected Error"...not sure where to go from here?
    – jaehmann
    Commented Mar 30, 2017 at 20:05
  • @jaehmann where is the Unexpected Error occuring? On the if 'Notes_SHLs'... line, or the one after?
    – Midavalo
    Commented Mar 30, 2017 at 20:12
  • It is occuring on the line after. The "layer.replaceDataSource(layer.dataSource, "FILEGDB_WORKSPACE", outdata_1)"
    – jaehmann
    Commented Mar 30, 2017 at 20:57
  • Looking at a similar Q&A here, try setting your outdata_1 to just be the feature class name, not the entire path
    – Midavalo
    Commented Mar 30, 2017 at 21:02
  • That seemed to work! I changed the outdata_1 to just be the feature class name not the entire path as well as instead of using "layer.dataSource" in the replaceDataSource line I plugged in the full path to the file GDB.
    – jaehmann
    Commented Mar 30, 2017 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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