I posted How to save just some mxds through 'if' in python? yesterday, and now I want to do a change in the script.
I changed the script, to run over sub-folders. It goes nice, but when it jumps another folder level, for the second time, I got an error:
Traceback (most recent call last):
File "D:\teste\change_jpg_layout_subfolder.py", line 18, in <module>
mxds = arcpy.mapping.MapDocument(MXDpath)
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 609, in __init__
assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.
There is nothing wrong with the foldernames and mxdnames. No special characters, no spaces, nothing. It just doesn't go further.
This is the code now:
import arcpy, os, glob
source = os.curdir
mxdList = []
for dirpath, subdirs, files in os.walk(source):
for x in files:
if x.endswith(".mxd"):
mxdList.append(os.path.join(dirpath, x))
print mxdList
for mxd in mxdList:
MXDpath = mxd
mxds = arcpy.mapping.MapDocument(MXDpath)
for elm in arcpy.mapping.ListLayoutElements(mxds, "PICTURE_ELEMENT"):
if elm.name == "logo":
elm.sourceImage = r"C:\logo\newlogo.jpg"
print MXDpath
mxds.save()
del mxds
Can you help me please, again? I also wanted to put the "mxds.save()" under a "try" conditional, that if for a reason the script couldn't save it, the script goes on... But I don't know how to do it.
This is my folder structure: