I am trying my hand at python for the first time. Using 2.7.18 Idle to check for errors (b/c I don't understand visual studio yet) I am wanting to go through all MXDs in a folder, find the layer Backup (shapefile) and change its source to a different folder and a GDB with another name, then update the TOC layer name. I don't get errors, but the script does not do anything. All my printed statements print but nothing seems to be working. Any suggestions for this newbie?
import arcpy
from arcpy import env
# Set the workspace for the folder containing MXDs
arcpy.env.workspace = r"S:\Workgroups\Test"
mxdList = arcpy.ListFiles("*.mxd")
# Iterate through all MXDs in the workspace
for mxd in mxdList:
mxdpath = env.workspace + "\\" + mxd
print (mxd + "Is being processed")
mxdNext = arcpy.mapping.MapDocument(mxdpath)
for df in arcpy.mapping.ListDataFrames(mxdNext,"Layers"):
for lyr in arcpy.mapping.ListLayers(mxd, "Backup", df):
if lyr == "Backup":
Lyr.replaceDataSource(r"S:\Workgroups", "SHAPEFILE_WORKSPACE", "Backup.shp",
r"S:\Workgroups\Data.gdb\Final", "FILEGDB_WROKSPACE")
if lyr.name == "Backup":
lyr.name = "Final"
print("lyr.name")
arcpy.RefreshTOC()
del mxd
print("Data sources updated for all MXDs.")
>"FILEGDB_WROKSPACE"
This might be the issue? Although tbh I feel like it'd throw an actual error at you.
good catch. I corrected that. Still no error or changes made.
Try mxdNext.save() instead.
line 18 you are trying to replaceDataSource on variable "Lyr", but your layer variable is set to lower case "lyr".
Is it just this capitalization typo that is causing it?
R_
@AlfredBaldenweck is correct.
If you're ever curious what a variable is you can type:
print(type(mxd))
Also, the df part need to be tabbed over:
from arcpy import env
# Set the workspace for the folder containing MXDs
arcpy.env.workspace = r"S:\Workgroups\Test"
mxdList = arcpy.ListFiles("*.mxd")
# Iterate through all MXDs in the workspace
for mxd in mxdList:
mxdpath = env.workspace + "\\" + mxd
print (mxd + "Is being processed")
mxdNext = arcpy.mapping.MapDocument(mxdpath)
for df in arcpy.mapping.ListDataFrames(mxdNext,"Layers"):
for lyr in arcpy.mapping.ListLayers(mxd, "Backup", df):
if lyr == "Backup":
lyr.replaceDataSource(r"S:\Workgroups", "SHAPEFILE_WORKSPACE", "Backup.shp",
r"S:\Workgroups\Data.gdb\Final", "FILEGDB_WROKSPACE")
if lyr.name == "Backup":
lyr.name = "Final"
print("lyr.name")
mxdNext.save()
arcpy.RefreshTOC()
Otherwise it's only going to run on the last MXD in the folder, not all the MXDs.
Okay fixed the Lyr to lyr- based on all the changes now I get one map whose time stamp in file Explorer appears to have done something but when I open it, it is still the same. Also, if I open any map and try and save it under the same name/folder etc it says I don't have permissions for my folder to save or it is too full neither one of which is the case. Thanks for all the help. I am sure I have a few errors causing all this. I thought putting print statements in would help me determine where it gets hung up but that does not seem to help.