While Loop hangs on SaveACopy

Discussion created by BrokenLegMike on Apr 9, 2012
Latest reply on Apr 10, 2012 by BrokenLegMike
I am trying to do the following:

  1. Loop through a directory of mxd's

  2. Save a backup of the in memory file to a new location (the drafts folder)

  3. Before the backup can be saved in the new location, I need to check to see if it already exists in the new location. If it does, then I want to rename the file with a "_1"

  4. If the "_1" file already exists, then I want to save a new file with "_2" and so on

  5. Do some more stuff after

I'm using a "while" loop to check for the existence of the file and then save the next increment of i. I keep getting the following error:

MapDocObject: Unable to save.  Check to make sure you have write access to the specified file and that there is enough space on the storage device to hold your document.

I think it's getting hung up on the "saveACopy', but I'm not sure why. I do not want to overwrite my output either. I want to make sure

My code is as follows:

folderPath = r"Z:\Test"
draftloc = r"Z:\Test\Draft"

for filename in glob.glob(os.path.join(folderPath, "*.mxd")):
    fullpath = os.path.join(folderPath, filename)

    mxd = arcpy.mapping.MapDocument(filename)

    if os.path.isfile(fullpath):
        basename, filename2 = os.path.split(fullpath)

        result_name = filename2
        i = 0
        while arcpy.Exists(draftloc + "\\" + result_name):
            result_name = draftloc + "\\" + shortname + "_" + str(i) + extension

I appreciate any suggestions