I have the following code which has successfully allowed me to fix broken data sources for mxds within a single folder. I cannot seem to combine arcpy.mapping successfully with os.Walk or arcpy.da.Walk in order to update the broken data source for all mxds within subdirectories of a given folder.

import arcpy
arcpy.env.workspace = r"W:\FakeFilePath"
for mxdname in arcpy.ListFiles("*.mxd"):
   print mxdname    
   mxd = arcpy.mapping.MapDocument(r"W:\FakeFilePath\\" + mxdname)
   for lyr in arcpy.mapping.ListBrokenDataSources(mxd):
      if lyr.supports("DATASOURCE"):
         if lyr.dataSource == r"G:\LandStatus.gdb\PLSS":         
            lyr.replaceDataSource(r"G:\PLSS.gdb", "FILEGDB_WORKSPACE", "PLSSFirst")
            lyr.name = "Sections"
            print lyr.name
            del mxd

I've been able to use os.Walk to print a list of all mxds within a tree of directories, but have not been able to combine that with the above code to do a bulk find/replace data source.

  • 1
    Personally I would use arcpy.da.Walk rather than os.Walk but in any event that seems to be where you are stuck so I think you need to include your attempt to use it within the code snippet that you present. Otherwise it sounds like you are just asking someone to put two bits of code (one not even presented as a snippet) together for you. – PolyGeo Jul 5 '17 at 23:55
  • Thanks PolyGeo, I will have to update the code snippet tomorrow. – Nathan Herzog Jul 5 '17 at 23:58

Your Answer

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

Browse other questions tagged or ask your own question.