1

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))
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Feb 19, 2018 at 14:53
3
  • 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. Commented Feb 19, 2018 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. Commented Feb 19, 2018 at 16:37
  • @Hornbydd I have re-opened this question - I think your comment should be turned into an answer. Commented Feb 19, 2018 at 17:49

1 Answer 1

1

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")):
 filenum+=1
my_next_file =filename+str(filenum)+".shp"
answered Feb 19, 2018 at 22:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.