2

I have a program (720 lines of code) that runs multiple tasks on Arcmap .mxd files. Such as update layers, update text, add layers, etc.

Within my program I have multiple "mxd.save()" instances. All of which occur within for loops.

For example:

for item in mxds:
    mxd = arcpy.mapping.MapDocument(item)
    df=arcpy.mapping.ListDataFrames(mxd,"Project Area")[0]
    print "HOWDY!"              
    mxd.save()  

Question is can I just have at the very end of my program have one for loop that saves all the mxds I was working on, while still retaining all the changes?

UPDATE:

Problem I think am having is this. I have one of my for loops that adds a layer to the map. If I do not mxd.save() after that layer is added. My program later when it attempts to find the layer that was added cannot find it. I guess it's not held in memory.

UPDATE2:

THANKS! I was doing something similar, but wrong none the less. Each time I had a for loop I would create a new .MapDocument object which I realized (thanks to you) was unnecessary. I now define the mapdocument object once at the start of my program and work from there. Do this has also greatly increased the speed of my program.

5

You sure can. As long as .save() is called on every map document object you have open before you close it (either by leaving a function if it's opened there or the script ends) your changes will persist. If you're working on one mxd at a time, though, I'd recommend saving each one as soon as you're done with it, just in case an error further down the line in the script prevents it from making it all the way to the part where it does all the saves at once.

Update after OP's update: mxds should be a list of MapDocument objects if you want it to work that way and NOT a list of strings that are paths to MXDs. Each time you create a MapDocument object it is its own entity independent of any other. What you REALLY want to do is something more like this:

mxdFiles = [r'c:\a.mxd', r'c:\b.mxd', r'c:\c.mxd']
mxds = [arcpy.mapping.MapDocument(mxdFile) for mxdFile in mxdFiles]
for mxd in mxds:
    df = arcpy.mapping.ListDataFrames(mxd,"Project Area")[0]
    print "HOWDY!"
# .. more code ..
for mxd in mxds:              
   mxd.save()  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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