How To: Use the arcpy.CreateUniqueName function with a tool that has the output name and output location parameters separated
The arcpy.CreateUniqueName data function creates a unique output name by adding integers to the output name. This helps avoid errors or unintentionally overwriting existing datasets. The return format of the data function includes the full path name, and some geoprocessing tools require that an output location and an output name are specified in separate parameters. The Feature Class to Feature Class geoprocessing tool is one example of this. In these instances, the unique name must be used without the full path name, and this can be done with Python.
The following code example shows how to eliminate the full path name from the return value and pass it into a tool such as the Feature Class to Feature Class tool.
#import arcpy and the OS module
import arcpy, os
from arcpy import env
#set workspace parameter
ws = env.workspace = r"C:\Temp\scratch.gdb"
#set input/output variables
inFeatures = "Customers"
outOriginal = "CustomerLocations"
#create unique name
unique_name = arcpy.CreateUniqueName(outOriginal)
#grab the basename of the full path returned
outName = os.path.basename(unique_name)
#pass outName into Feature Class to Feature Class
arcpy.FeatureClassToFeatureClass_conversion(inFeatures, ws, outName)