Python - Save output files with unique names

1374
2
06-29-2018 07:57 AM
MikeO_Shell1
New Contributor II

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. >>>

Tags (2)
0 Kudos
2 Replies
RandyBurton
MVP Alum

One way would be to append date/time.

import arcpy
from datetime import datetime 

saveLayer = "{}_{}".format('observation_Layer',datetime.now().strftime('%y%m%d%H%M%S'))

# observation_Layer_180629093021

arcpy.MakeNetCDFFeatureLayer_md(
    ....
    out_feature_layer = saveLayer,
    .... )‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
JoshuaBixby
MVP Esteemed Contributor

If you don't need to keep the results from previous runs, there is overwriteOutput from env—Help | ArcGIS Desktop .  If overwriting is not an option, there is CreateScratchName—Help | ArcGIS Desktop and CreateUniqueName—Help | ArcGIS Desktop