I am currently working on a python script where I am trying to copy selected features into a feature dataset.

Basically I have an mxd containing some layers, and for each layer I select features by their attributes, this query is based on the featuredataset name. The script I have written get a list of the feature datasets of my geodatabase. With the name of the featuredataset, I can build a query filter (arcpy.SelectLayerByAttribute_management) for each layers of the mxd. If my selection return something, I export the selected feature in the proper dataset (arcpy.FeatureClassToFeatureClass_conversion). The script is processing the first dataset properly, but once it jumps to the next one it fails... I cannot figuring out why I am receiving the following message error(arcgisscripting.ExecuteError: ERROR 999999: Error executing function. Failed to execute (FeatureClassToFeatureClass).)

##path of the geodatabase 
##set the workspace
##get the list of the featuredataset
fdlist =arcpy.ListDatasets()
##get the mxd path and initialise it
doc_mxd = arcpy.mapping.MapDocument(path_mxd)
##list of layers

for fds in fdlist:
    print "________","\n ", fds
    ##work on the featuredataset name, for suiting the query
    get_fds= fds.replace("As_con_","")
    ##path of the featuredataset
    ##prepare the query
    SQLexpression= "\"As_Constructed_Plan\"='"+fds_name+"'"
                    ##path where we will store the selection

    for fc in fclist:
        ##Select the featureclasses
        arcpy.SelectLayerByAttribute_management (fc,"NEW_SELECTION",SQLexpression)
        ##Count the selected features
        count = int(arcpy.GetCount_management(fc).getOutput(0))
        print fc,count
        ##if the query succeed, we will copy the  selection into the proper featuredataset
        if count>0:
            ##print some info

            ##check if the featureclass does not exist
            if not arcpy.Exists(fc_path):
                print "\n copying...",count," entities , on ",fc_path
                print (fc_copy_from,fds_path_test,fc_name,SQLexpression)

                print "export ok"
  • 1
    Are you familiar with try..except blocks? Is it possible that the next feature class has the same name as the first? You can test with arcpy.exists(fds_path_test+"\\"+fc_name) see resources.arcgis.com/en/help/main/10.2/index.html#//…. A copy of the outputs may help (turn on quick edit in the command window, select the area and right click then paste into notepad) or screen grab. Commented Sep 8, 2014 at 5:20
  • It looks like you are looping through the output feature datasets and storing the feature class more than once (once in each feature dataset) hence the error - names must be unique in the database, not just the dataset. If there is only one feature dataset in the output database gdb_hull = "D:\\As_Con_Plans\\As_Con_Hull_part1.gdb" then I'll need to look harder. Check also for spaces and non-alphanumeric characters in the layer names, that will also cause that error. Commented Sep 8, 2014 at 5:58

2 Answers 2


Finally, I figured out that my problem was linked to the name of the feature class' output. It is the fc_name of the following line:


I have run some test and discovered that I can't add 2 similar output name... even if it is processing a different dataset... So to fix the problem, I have created an incremented index that I linked to the feature class name (new_fc_name=fc_name+str(i) with I incremented after a loop). It may not be the best way to process, but at least, it is working...


You can have 2 check:

  1. Check if name is valid

    vname = arcpy.ValidateTableName(fc_name,wksp)

  2. Check if name exist

    def validUniqueName(name, wk):
        """ Create a unique name not exist in GDB
        Note: you can fix limit to number of pas and add test if name is too long"""
        check = True
        while check:
            check = arcpy.Exists(os.path.join(wk, name))
            if check:
                name = "{}_1".format(name)
        return name

Add 2 lines in function:

vname = arcpy.ValidateTableName(fc_name,wksp)
v_unique_name = validUniqueName(vname,wksp)


>>> name = u"Voirie etroite"
>>> name
u'Voirie etroite'
>>> vname = arcpy.ValidateTableName(name,wk)
>>> vname
>>> vuname = validUniqueName(vname,wk)
>>> vuname

Your Answer

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

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