I'm trying to do something quite simple but its not working. I want my python script to change all of the layers that point to the same datasource (a point shapefile) to another point shapefile. replaceDataSource seems to be the methods but when I run this:

for lyr in arcpy.mapping.ListLayers(mxd):
    #print lyr.name
    if lyr.dataSource == r"X:\filepath\folder\points.shp":
            print lyr.dataSource
            lyr.replaceDataSource(r"\\main.glb.corp.local\E-GB$\Home\AB\4\J022414\Documents\ArcGIS", 'SHAPEFILE_WORKSPACE', "ismage_template_P.shp")
            print "data replaced"

i get "Runtime error type 'exceptions.ValueError'>: Layer: Unexpected error" when I run this. It prints the data source but doesnt print "data replaced" so it looks like the problem is on the lyr.replacedatasource line

Has anyone had this problem before, or should I be using a different method?

  • i am doing the same thing with PGDB(MDB), same error occurs everytime. Commented Aug 26, 2016 at 10:22

1 Answer 1


Your lyr.replaceDataSource already knows that it is working with a SHAPEFILE_WORKSPACE and so it does not need ".shp " (which instead upsets it).

If you leave it off then I think your code will work.

  • Thanks, that seems to have worked, I have run into another problem thought. I dont know if its related "Runtime error <type 'exceptions.AttributeError'>: LayerObject: Get attribute dataSource does not exist". Any idea? Commented Mar 28, 2014 at 12:49
  • Oh never mind I fixed it by adding "if lyr.supports("DATASOURCE"):" Commented Mar 28, 2014 at 13:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.