The loop runs once, then i get this error message.

Runtime error  Traceback (most recent call last):   File "<string>", line 17, in <module>   File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\management.py", line 6997, in MakeFeatureLayer     raise e ExecuteError: ERROR 000725: Output Layer: Dataset out_VHQ_7 already exists.

To make this code work i think i need to add something to the outputname "out_VHQ_7" to avoid that when the loop runs the second time it stops because it cannot overwrite the output that has been createt in the first run of the loop

>>> FieldToUse = "Symbol" # change this to your field name with the unique values
... for fc in fcList:
...     uVals = [] # new empty list
...     # loop through each feature with a searchcursor
...     with arcpy.da.SearchCursor(fc,FieldToUse) as SCur:
...         for row in SCur:
...             # check if the value is already in the list
...             if row[0] not in uVals:
...                 uVals.append(row[0]) # add new value to list
...     #loop through each unique field value
...     for FieldValue in uVals:
...         # create a layer with a where clause, this is for numeric fields
...         arcpy.MakeFeatureLayer_management(fc,"out_VHQ_7","{} = \'{}\'".format(FieldToUse,FieldValue))
...         # use "{} = \'{}\'".format(FieldToUse,FieldValue) to quote the value
...         # if the input field is a string field
...         OutTIFFname = "{}_{}_kd.tif".format(os.path.splitext(fc)[0],FieldValue)
...         outKDens = KernelDensity("out_VHQ_7","NONE",4670)
...         outKDens.save(os.path.join(outworkspace,OutTIFFname))
  • Please edit your question to include some more information - what are you trying to do, and what exactly is happening when you run your code? Does it output anything at all, or does it give an error after it has looped? Please include the full error message as text in your question. – Midavalo Feb 19 '18 at 15:36
  • 2
    The error is referring to the make featurelayer tool and telling that layer "out_VHQ_7" already exists. Which makes sense as you never change the name of the layer you are making. I would put a arcpy.Delete_management("out_VHQ_7") after you save out your raster. – Hornbydd Feb 19 '18 at 16:37
  • @Hornbydd I have re-opened this question - I think your comment should be turned into an answer. – Midavalo Feb 19 '18 at 17:49

If you dont care about the output, You can either delete the prior output before running the loop again as suggested in an above comment or you can enable overwrite.

If you want to keep that file you can append an ascending number to the end of the file name with something like this (this worked for me in a different task, but I'm not a python pro so there is likely a cleaner method out there)

import os.path as pth

myfile ='out_VHQ_'
filename = myfile
filenum = 1

#check if output filename already exists and, if so, append +1 suffix
while (pth.exists(filename+str(filenum)+".shp")):
my_next_file =filename+str(filenum)+".shp"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.