Help with repairing data source

549
6
06-20-2011 06:52 AM
OliverMorris
New Contributor III
Hi,

I have been trying to get this code to work for the last couple of hours, if anyone could give me some help that would be great.

I would like to repoint any broken links to a new table that lies within a personal geodatabase. The code I have is:

import arcpy, os
path = r"N:\Temporary_Work\Oliver Morris\EDCOLLINS_ISOPACH"
for fileName in os.listdir(path):
    fullPath = os.path.join(path, fileName)
    if os.path.isfile(fullPath):
        basename, extension = os.path.splitext(fullPath)
        if extension == ".mxd":
            mxd = arcpy.mapping.MapDocument(fullPath)
            print "MXD: " + fileName
            brknList = arcpy.mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
                 lyr.replaceDataSource (r" N:\Temporary_Work\mapping_2011.mdb",        "ACCESS_WORKSPACE", SEA_110617)
                 print "\t" + brknItem.name
del mxd


When I run this I get the error - Runtime error <type 'exceptions.NameError'>: name 'SEA_110617' is not defined

Can you please point me in the right directions.

Thanks,

Oliver
0 Kudos
6 Replies
JeffBarrette
Esri Regular Contributor
SEA_110617 is the name of a feature class, not a variable.  It therefore should be in quotes.  Change it to "SEA_110617".

Your code below is changing all broken layers to be the same source.  Is that what you really want.

Also note - if SEA_110617 is a raster dataset you may not be able to repair it.  We just found an issue with rasters.  We will address it at SP3.

import arcpy, os
path = r"N:\Temporary_Work\Oliver Morris\EDCOLLINS_ISOPACH"
for fileName in os.listdir(path):
    fullPath = os.path.join(path, fileName)
    if os.path.isfile(fullPath):
        basename, extension = os.path.splitext(fullPath)
        if extension == ".mxd":
            mxd = arcpy.mapping.MapDocument(fullPath)
            print "MXD: " + fileName
            brknList = arcpy.mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
                lyr.replaceDataSource (r" N:\Temporary_Work\mapping_2011.mdb", "ACCESS_WORKSPACE", SEA_110617)
                print "\t" + brknItem.name
del mxd
0 Kudos
OliverMorris
New Contributor III
Hi,

Thanks for your reply, when I try to run this I now get the error below

>>> import arcpy, os
... path = r"N:\Temporary_Work\Oliver Morris\EDCOLLINS_ISOPACH"
... for fileName in os.listdir(path):
...     fullPath = os.path.join(path, fileName)
...     if os.path.isfile(fullPath):
...         basename, extension = os.path.splitext(fullPath)
...         if extension == ".mxd":
...             mxd = arcpy.mapping.MapDocument(fullPath)
...             print "MXD: " + fileName
...             brknList = arcpy.mapping.ListBrokenDataSources(mxd)
...             for brknItem in brknList:
...   lyr.replaceDataSource (r"N:\Temporary_Work\Oliver Morris\EDCOLLINS_ISOPACH\Isopach_mapping_2011.mdb","ACCESS_WORKSPACE","SEA_ODM_ISOPACH_110617")
... print "\t" + brknItem.name
... del mxd
... 
MXD: ISOPACH_EDIT1_EDCOLLINS.mxd

Runtime error <type 'exceptions.NameError'>: name 'lyr' is not defined
>>> 


Any idea why this is not working.

Many Thanks,

Oliver
0 Kudos
OliverMorris
New Contributor III
I have also tried this as well but get the following error:

import arcpy, os
path = r"N:\Temporary_Work\Oliver Morris\EDCOLLINS_ISOPACH"
for fileName in os.listdir(path):
    fullPath = os.path.join(path, fileName)
    if os.path.isfile(fullPath):
        basename, extension = os.path.splitext(fullPath)
        if extension == ".mxd":
            mxd = arcpy.mapping.MapDocument(fullPath)
            print "MXD: " + fileName
            brknList = arcpy.mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
  for lyr in brknItem:
   lyr.replaceDataSource (r" N:\Temporary_Work\Oliver Morris\EDCOLLINS_ISOPACH\Isopach_mapping_2011.mdb","ACCESS_WORKSPACE","SEA_ODM_ISOPACH_110617")
        print "\t" + brknItem.name
del mxd


but get the error:

MXD: ISOPACH_EDIT1_EDCOLLINS.mxd

Runtime error <type 'exceptions.AttributeError'>: LayerObject: Get attribute layers does not exist

Many Thanks for your help
0 Kudos
JeffBarrette
Esri Regular Contributor
That is most likely because you have a layer type (e.g., group layer) that doesn't support "replaceDataSource".  You should look at the help topic for layers, specifically the "supports" parameters.  It explains all of this.  I modified the code below (and removed an unnecessary if statement).

import arcpy, os
path = r"N:\Temporary_Work\Oliver Morris\EDCOLLINS_ISOPACH"
for fileName in os.listdir(path):
    fullPath = os.path.join(path, fileName)
    if os.path.isfile(fullPath):
        basename, extension = os.path.splitext(fullPath)
        if extension == ".mxd":
            mxd = arcpy.mapping.MapDocument(fullPath)
            print "MXD: " + fileName
            brknList = arcpy.mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
                if brknItem.supports("DATASOURCE"):
                    lyr.replaceDataSource(r" N:\Temporary_Work\Oliver Morris\EDCOLLINS_ISOPACH\Isopach_mapping_2011.mdb","ACCESS_WORKSPACE","SEA_ODM_ISOPACH_110617")
                    print "\t" + brknItem.name
del mxd
0 Kudos
OliverMorris
New Contributor III
Hi,

Thank you for your continued help with this. I ran your code but it returned an error any idea why.

Many Thanks,

Oliver

>>> import arcpy, os
... path = r"N:\Temporary_Work\Oliver Morris\EDCOLLINS_ISOPACH"
... for fileName in os.listdir(path):
...     fullPath = os.path.join(path, fileName)
...     if os.path.isfile(fullPath):
...         basename, extension = os.path.splitext(fullPath)
...         if extension == ".mxd":
...             mxd = arcpy.mapping.MapDocument(fullPath)
...             print "MXD: " + fileName
...             brknList = arcpy.mapping.ListBrokenDataSources(mxd)
...             for brknItem in brknList:
...                 if brknItem.supports("DATASOURCE"):
...                     lyr.replaceDataSource(r" N:\Temporary_Work\Oliver Morris\EDCOLLINS_ISOPACH\Isopach_mapping_2011.mdb","ACCESS_WORKSPACE","SEA_ODM_ISOPACH_110617")
...                     print "\t" + brknItem.name
... del mxd
...
MXD: ISOPACH_EDIT1_EDCOLLINS.mxd

Runtime error <type 'exceptions.NameError'>: name 'lyr' is not defined
0 Kudos
JeffBarrette
Esri Regular Contributor
The error message says it all "lyr" not defined.

You have lyr.replaceDataSource when it should be brknItem.replaceDataSource

Jeff
0 Kudos