# Saving output files with unique names to avoid ERROR 000725 from ArcPy?

I'm a GIS Analyst who is VERY new to Python. I have successfully run the script below. However, when I want to run it again with a different input file it gives me the 000725 error...output file already exists.

How do I automatically create a unique output filename every time I run this script?

>>> #ADD NETCDF FILE POINTS TO THE MAP
... arcpy.MakeNetCDFFeatureLayer_md(in_netCDF_file="Z:/ENGINEERING/Stormwater/Stormwater Files/Rainfall/NWS NetCDF Files/nws_precip_1day_20180625_netcdf/nws_precip_1day_20180625_conus.nc", variable="observation", x_variable="x", y_variable="y", out_feature_layer="observation_Layer", row_dimension="x;y", z_variable="", m_variable="", dimension_values="", value_selection_method="BY_VALUE")
...
Runtime error  Traceback (most recent call last):   File "<string>", line 2, in <module>   File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\md.py", line 125, in MakeNetCDFFeatureLayer     raise e ExecuteError: ERROR 000725: Output Feature Layer: Dataset observation_Layer already exists.
>>>

• Change out_feature_layer="observation_Layer" to a new value. How you come up with a new value is up to you, there are several options.. check if the file exists and add a 1 to the end of the name until a file doesn't exist is a good one. You don't need to implicitly specify your parameters if you are going in order, only if you want to skip one or more optional parameters or specify in a different order than expected by the tool.. that will save you a lot of typing. – Michael Stimson Jun 29 '18 at 14:55
• There is a tool for creating unique output names, although I usually do something that has more meaning than just a sequence. – Evil Genius Jun 29 '18 at 14:57
• That's another good method @EvilGenius, let the tool do the decisions for you. Giving it a meaningful base name helps otherwise you get a layer name in hex digits or a GUID.. It looks like though you'd have to specify parameters like "observation_Layer" , arcpy.env.workspace because it's not specified in the minimum code block. – Michael Stimson Jun 29 '18 at 15:00
• use datetime to get the time and date you run the script, convert that to a string, make a variable called output_name that holds the first part of the name, then concatenate the output_name + the string_time and you will have a unique name at each run. and you will also know when the script ran so you always know the most recent., – ed.hank Jun 29 '18 at 15:03
• I like to import the datetime library and append the current date at the end of the file name – NULL.Dude Jun 29 '18 at 15:43

One method I use to create unique file names is to append the current date to the end of the filename.

Sample code is below:

import os
import datetime
now = datetime.datetime.now()
currentDate = str(now.month) + "_" + str(now.day) + "_" + str(now.year)

file_output = os.path.join(r"C:\bla\bla\path\to\folder\filename_" + currentDate)


so if you execute the script on January 15th, 2018 with a basename of "apples" your final filename output will be "apples_1_15_2018"

• This is how I would do it. Very simple and effective. – ed.hank Jun 29 '18 at 16:03
• A more (internationally) standard way would be YYYYMMDD (alternatively with hyphens or underscores). This will also help when you sort alphabetically. – Stefan Jun 29 '18 at 20:18
• true, this is just a method i use at the local shop, no standrds required but you're correct. – NULL.Dude Jun 29 '18 at 21:09
• what error did you get? – NULL.Dude Jul 3 '18 at 15:57
• Thank you all for your help. I tried the sample code above and still having some trouble. The code is probably fine, but I'm brand new to Python (and programming for that matter). I did what I thought I should with the code and got the following error below. Feel free to dumb-down your answers as much as possible - I won't be insulted :) – Mike Jul 3 '18 at 18:06

I usually use tempfile

import tempfile

my_unique_file = tempfile.mktemp(preffix="filename-", suffix=".tiff", dir="/tmp/myoutput/dir")