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

独自空忆成欢 提交于 2019-12-11 11:35:24

问题


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


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/10036489/copy-a-file-to-a-new-location-and-increment-filename-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!