Copy a file to a new location and increment filename, python

Go To StackoverFlow.com

1

I am trying to:

  1. Loop through a bunch of files
  2. makes some changes
  3. Copy the old file to a sub directory. Here's the kicker I don't want to overwrite the file in the new directory if it already exists. (e.g. if "Filename.mxd" already exists, then copy and rename to "Filename_1.mxd". If "Filename_1.mxd" exists, then copy the file as "Filename_2.mxd" and so on...)
  4. save the file (but do a save, not a save as so that it overwrites the existing file)

it goes something like this:

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)


    # Make some changes to my file here

    # Copy the in memory file to a new location. If the file name already exists, then rename the file with the next instance of i (e.g. filename + "_" + i)

    for i in range(50):
        if i > 0:
            print "Test1"
            if arcpy.Exists(draftloc + "\\" + filename2) or arcpy.Exists(draftloc + "\\" + shortname + "_" + str(i) + extension):
                print "Test2"
                pass
            else:
                print "Test3"
                arcpy.Copy_management(filename2, draftloc + "\\" + shortname + "_" + str(i) + extension)
    mxd.save()

So, 2 things I decided to do, was to just set the range of files well beyond what I expect to ever occur (50). I'm sure there's a better way of doing this, by just incrementing to the next number without setting a range.

The second thing, as you may see, is that the script saves everything in the range. I just want to save it once on the next instance of i that does not occur.

Hope this makes sense,

Mike

2012-04-05 21:46
by Mike


0

thanks to Maty suggestion above, I've come up with my answer. For those who are interested, my code is:

    result_name = filename2
    print result_name
    i = 0

    # Check if file exists
    if arcpy.Exists(draftloc + "\\" + result_name):
        # If it does, increment i by 1
        i+=1
        # While each successive filename (including i) does not exists, then save the next filename
        while not arcpy.Exists(draftloc + "\\" + shortname + "_" + str(i) + extension):                
            mxd.saveACopy(draftloc + "\\" + shortname + "_" + str(i) + extension)            
    # else if the original file didn't satisfy the if, the save it.
    else:           
        mxd.saveACopy(draftloc + "\\" + result_name)
2012-04-10 16:37
by Mike


4

Use a while loop instead of a for loop. Use the while loop to find the appropriate i, and then save afterwards.

The code/pseudocode would look like:

result_name = original name
i = 0
while arcpy.Exists(result_name):
    i+=1
    result_name = draftloc + "\\" + shortname + "_" + str(i) + extension
save as result_name

This should fix both issues.

2012-04-05 22:19
by Maty
I tried this and it crashes when the save takes place. Not really sure why.. - Mike 2012-04-10 15:08
Ads