Note that there are some explanatory texts on larger screens.

plurals
  1. POCopy a file to a new location and increment filename, python
    primarykey
    data
    text
    <p>I am trying to:</p> <ol> <li>Loop through a bunch of files</li> <li>makes some changes</li> <li>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...)</li> <li>save the file (but do a save, not a save as so that it overwrites the existing file)</li> </ol> <p>it goes something like this:</p> <pre><code>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 &gt; 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() </code></pre> <p>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. </p> <p>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.</p> <p>Hope this makes sense,</p> <p>Mike</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload