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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 30, 2017 at 16:37
5
  • 1
    Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. Commented Mar 30, 2017 at 17:01
  • 1
    Does it print this line print "{} -> {}".format(layer.name, layer.dataSource)? 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 Commented Mar 30, 2017 at 17:02
  • 1
    Please edit your question to include any clarification in response to comments 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? Commented Mar 30, 2017 at 18:48

1 Answer 1

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
answered Mar 30, 2017 at 18:55
6
  • 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? Commented Mar 30, 2017 at 20:05
  • @jaehmann where is the Unexpected Error occuring? On the if 'Notes_SHLs'... line, or the one after? Commented Mar 30, 2017 at 20:12
  • It is occuring on the line after. The "layer.replaceDataSource(layer.dataSource, "FILEGDB_WORKSPACE", outdata_1)" 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 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. Commented Mar 30, 2017 at 21:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.