1

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:

folder structure

1 Answer 1

2

You haven't looped through your subfolders. You've only looped through the files in the source.

for dirpath, subdirs, files in os.walk(source):   
    for folders in subdirs:
        for x in files:
            if x.endswith(".mxd"):
                    mxdList.append(os.path.join(dirpath, x))

In addition, you have indented the "del mxds" incorrectly. Make sure you delete the variable at the end of the first loop. Also, you don't need to create the variable "MXDpath" when "mxd" can be used. Try:

for mxd in mxdList:
    mxds = arcpy.mapping.MapDocument(mxd)
    for elm in arcpy.mapping.ListLayoutElements(mxds, "PICTURE_ELEMENT"):
        if elm.name == "logo":
            elm.sourceImage = r"C:\logo\newlogo.jpg"
            print mxd 
            mxds.save()
    del mxds
6
  • Thanks, i changed it. There is no error now, but it still doesnt read the 3rd level folder. Just the root, and the second level...
    – raphael
    Commented May 11, 2017 at 13:21
  • It should read all the subfolders in the directory. Can you put up the folder structure?
    – MacroZED
    Commented May 11, 2017 at 13:26
  • I've added a loop for the directory too. Check above. It might work, but not sure.
    – MacroZED
    Commented May 11, 2017 at 13:29
  • I put the folder structure, in the questionl. Look that, please. Well, now it got stuck at the second level. Did the change several times, at the mxds in the second level folder, and didt change to the 3rd. Just ended.
    – raphael
    Commented May 11, 2017 at 13:33
  • I tried again, without the "for dirs in dirpath:" and it worked! maybe i did something wrong before... i thing is working fine now!
    – raphael
    Commented May 11, 2017 at 13:47

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.