2

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

try:
    arcpy.CopyFeatures_management('Notes_SHLs', 'JeffNotes_SHLs')
    print "SHLs Feature Class Created Succesfully!"
except:
    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)

        mxd.save()

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 Mar 30 '17 at 17:01
  • 1
    Does it print this line print "{} -> {}".format(layer.name, layer.dataSource)? – Midavalo Mar 30 '17 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 Mar 30 '17 at 17:02
  • 1
    Please edit your question to include any clarification in response to comments – Midavalo Mar 30 '17 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 Mar 30 '17 at 18:48
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 Mar 30 '17 at 20:05
  • @jaehmann where is the Unexpected Error occuring? On the if 'Notes_SHLs'... line, or the one after? – Midavalo Mar 30 '17 at 20:12
  • It is occuring on the line after. The "layer.replaceDataSource(layer.dataSource, "FILEGDB_WORKSPACE", outdata_1)" – jaehmann Mar 30 '17 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 Mar 30 '17 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 Mar 30 '17 at 21:29

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.