I'm trying to write a script that allows me to look through map documents and replace each layer's data source in each map document. Everything is working logically I believe, and all my loops are working as they should. The datasets that are referenced exist. Yet, I am getting this error :

Traceback (most recent call last): File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec codeObject in main.dict File "C:\Users\Daimon Nurse\Desktop\DFMPROJECT\Scripts\editmapdocument8.py", line 18, in Lyr.replaceDataSource(workspace_path, workspace_type, dataset_name) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy_mapping.py", line 680, in replaceDataSource return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True))) ValueError: Layer: Unexpected error

This is the code I am using:

 import arcpy
import os

PATH2 = r"C:\Users\Daimon Nurse\Desktop\DFMPROJECT"
arcpy.env.workspace = PATH2

for file in arcpy.ListFiles("*.mxd"):
    filePath = os.path.join(PATH2,file)
    MapDoc = arcpy.mapping.MapDocument(filePath)
    lyrList = arcpy.mapping.ListLayers(MapDoc)
    i = 1
    for Lyr in lyrList:
        print Lyr 
        workspace_path =  r"C:\Users\Daimon Nurse\Desktop\DFMPROJECT\DFM"
        workspace_type = "FILEGDB_WORKSPACE"
        dataset_name = 'Zone{0}'.format(i)
        print dataset_name
        print workspace_type
        print workspace_path
        print os.path.join (workspace_path, dataset_name)
        Lyr.replaceDataSource(workspace_path, workspace_type, dataset_name)
    i = i + 1

These are the printed results on the first iteration for dataset_name, workspace_type, workspace_path and os.path.join respectively:

C:\Users\Daimon Nurse\Desktop\DFMPROJECT\DFM
C:\Users\Daimon Nurse\Desktop\DFMPROJECT\DFM\Zone1
  • The code looks right. Before Lyr.replaceDataSource(workspace_path, workspace_type, dataset_name), try print os.path.join (workspace_path, dataset_name). Copy the path printed and paste it into the ArcCatalog file path to make sure what you're referencing is really there. If ArcCatalog can't find the feature class, that's your problem. – Emil Brundage Jan 10 '15 at 3:14
  • Yup, did that, ArcCatalog has it. – Daimon Nurse Jan 10 '15 at 3:22
  • How about setting the workspace variable as such: workspace_path = r"C:\Users\Daimon Nurse\Desktop\DFMPROJECT\DFM"? Remove the extension since you indicate the workspace type. – Emil Brundage Jan 10 '15 at 3:35
  • Tried it, same error. – Daimon Nurse Jan 10 '15 at 4:04
  • 1
    It sounds like Zone1, for example, is a Feature Dataset, meaning a collection of Feature Classes within a geodatabase. If this is so, I would try out MapDoc.findAndReplaceWorkspacePaths. The first variable being the old path, and the second being the replacement. help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… – Emil Brundage Jan 12 '15 at 2:45

The problem was I was listing the dataset in the replacedatasource function when I really just needed to list the name of the feature class itself. for example:

for table in arcpy.mapping.ListTableViews(mxd):
    print table.name
    filename1 = table.name
    table.replaceDataSource(PATH, workspacetype, filename1)
    print "REPLACED!"

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.