[INDENT]import arcpy sourceType = ".mxd" # Change this to .lyr and it fails. fullpath = r"D:\WD\InterimGroundwater\Salinity" + sourceType sourceLyr = arcpy.mapping.MapDocument(fullpath) ID = "10014087" newAquiferBoundary = "ID_" + ID + "_AquiferBoundary" newLyr = r"D:\WD\InterimGroundwater\Salinity_" + ID + sourceType lyrList = arcpy.mapping.ListLayers(sourceLyr) for lyr in lyrList: if lyr.supports("DATASOURCE"): if lyr.name == "AquiferBoundary" : print lyr.dataSource lyr.replaceDataSource(r"D:\\WD\\InterimGroundwater\\AHGF_GW_v1-0_master.gdb", "FILEGDB_WORKSPACE", newAquiferBoundary) sourceLyr.saveACopy(newLyr)[/INDENT]
I think your problem is with your source layer declaration. (sourceLyr = arcpy.mapping.MapDocument(fullpath)) In this statement, you need to be declaring a map document (mxd) and so that's why when you change the extension to .lyr, it crashes.I'd suggest:mxd = arcpy.mapping.MapDocument(fullpath)sourceLyr = arcpy.mapping.ListLayers(mxd, "name of layer", "data frame")[0]and then carry on with declaring the ID and the new layer, etc.
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.