0

I have a script that works, but when I run it a second time it doesn't because it keeps saving the output filename the same. I'm very new to Python and programming in general, so dumb you answers down...and then dumb them down some more. :)

arcpy.gp.Spline_sa("Observation_RegionalClip_Clip", "observatio", "C:/Users/moshell/Documents/ArcGIS/Default.gdb/Spline_shp16", "514.404", "REGULARIZED", "0.1", "12")

Where Spline_shp16 is the output filename, I would like it to save as Spline_shp17 the next time I run the script, and then Spline_shp18 the time after that, etc.

Rick Smith
  • 3,962
  • 6
  • 13
  • 24
Mike
  • 1
  • 1
  • if overwriting an output file is a concern, I typically take one of two approaches; either include a timestamp in the output filename, or make the output filename a required script argument. In the first case, you will avoid producing duplicate output filenames, while in the second case you can offload the avoidance of duplicates to whatever is calling the script in the first place (e.g. the user, another program, a workflow manager, etc). This avoids the situation and keeps your code simpler. – user5359531 Feb 28 '19 at 02:04
  • an example of using a timestamp like I mention could look like this: `import time; output_file = "foo.{0}.txt".format(int(time.time()))`, yielding `foo.1551319949.txt` – user5359531 Feb 28 '19 at 02:13
  • Possible duplicate of [How do I create a incrementing filename in Python?](https://stackoverflow.com/questions/17984809/how-do-i-create-a-incrementing-filename-in-python) –  Feb 28 '19 at 02:19
  • Thank you for your response. The timestamp approach would be ideal. However, I'm still just learning programming and am not sure exactly where I would place the example code you have listed above. – Mike Feb 28 '19 at 16:09

1 Answers1

0

If you want to use numbers in the file names, you can check what files with similar names already exist in that directory, take the largest one, and increment it by one. Then pass this new number as a variable in the string for the filename.

For example:

import glob
import re

# get the numeric suffixes of the appropriate files
file_suffixes = []
for file in glob.glob("./Spline_shp*"):
    regex_match = re.match(".*Spline_shp(\d+)", file)
    if regex_match:
        file_suffix = regex_match.groups()[0]
        file_suffix_int = int(file_suffix)
        file_suffixes.append(file_suffix_int)


new_suffix = max(file_suffixes) + 1 # get max and increment by one
new_file = f"C:/Users/moshell/Documents/ArcGIS/Default.gdb/Spline_shp{new_suffix}" # format new file name

arcpy.gp.Spline_sa(
    "Observation_RegionalClip_Clip",
    "observatio",
    new_file,
    "514.404",
    "REGULARIZED",
    "0.1",
    "12",
)

Alternatively, if you are just interested in creating unique filenames so that nothing gets overwritten, you can append a timestamp to the end of the filename. So you would have files with names like "Spline_shp-1551375142," for example:

import time

timestamp = str(time.time())
filename = "C:/Users/moshell/Documents/ArcGIS/Default.gdb/Spline_shp-" + timestamp
arcpy.gp.Spline_sa(
    "Observation_RegionalClip_Clip",
    "observatio",
    filename,
    "514.404",
    "REGULARIZED",
    "0.1",
    "12",
)
Henry Woody
  • 14,024
  • 7
  • 39
  • 56
  • Thank you so much for your response. I tried the timestamp code and it doesn't like the curly braces around timestamp. I tried a couple different braces but they did not work either. – Mike Mar 01 '19 at 16:17
  • @Mike I made an update to the timestamp stuff, now just using normal string concatenation, try that out – Henry Woody Mar 01 '19 at 17:53