I wrote an ArcMap script to go through a feature layer, identify unique values in a given field, and output a feature class from each unique value to its own feature class or shapefile. As an exercise, I decided to experiment with allowing the user to set a limit on the number of outputs, so the script would only take the first 10 or 20 unique values and create a feature class from each in a designated workspace.
However, if there are more unique values than the limit, the script deletes whatever geodatabase or folder is designated as its output location, along with any files in that location. Occasionally, the script will throw a 000210 error when creating its first file for no reason that I can determine (the workspace is not open in ArcCatalog or ArcMap, the file name is valid, and nobody else in my agency is using it) and if this happens, the output location is also deleted.
Below is my script in its entirety, except for a header that is all comments. Has anyone experienced similar behavior from a script? Any ideas why this is happening? I've written a number of scripts, both stand-alone and added to ArcMap, and I'm terrified to write any more until I know why this one is acting so oddly.
import arcpy, time, os
from arcpy import env
#set parameters
# SplitFeat - input features
SplitFeat = arcpy.GetParameterAsText(0)
# SplitField - input field
SplitField = arcpy.GetParameterAsText(1)
# OutLocation - output folder
OutLoc = arcpy.GetParameterAsText(2)
#MaxSplit = Maximum number of files to create
MaxSplit = arcpy.GetParameterAsText(3)
# Overwrite output
arcpy.env.workspace = OutLoc
arcpy.env.overwriteOutput = True
IndNum = 0
arcpy.MakeFeatureLayer_management(SplitFeat, "Split_lyr")
CatList = []
CodeList = []
FldList = (SplitField)
with arcpy.da.SearchCursor("Split_lyr", SplitField) as cursor:
for row in cursor:
if row[0] not in CatList and IndNum < MaxSplit:
AttString = str(row[0])
print AttString
CatList.append(AttString)
CodeString = ''.join(e for e in AttString if e.isalnum())
CodeList.append(CodeString)
IndNum = IndNum + 1
elif IndNum == MaxSplit:
arcpy.AddMessage("{0} classes identified. Proceeding with script...")
else:
"All classes identified. Proceeding with script..."
#splitting features
#new iteration number
IterNum = 0
for Cat in CatList:
CodeName = CodeList[IterNum]
ClassName = CodeName[0:30]
ClassFile = os.path.join(OutLoc, ClassName)
arcpy.AddMessage("Creating file {0}...".format(ClassFile))
#select by attributes
SelClause = "{0!s} = '{1!s}'".format(SplitField, Cat)
arcpy.SelectLayerByAttribute_management("Split_lyr", "NEW_SELECTION", SelClause)
#Copy features, using CodeList index as key
arcpy.CopyFeatures_management("Split_lyr", ClassFile)
arcpy.AddMessage("{0} has been created.".format(ClassFile))
IterNum = IterNum + 1
feature class to feature class (conversion)
orselect (analysis)
.select
is good because you can skip the priorselect layer by attribute
.Select_analysis
and had the same problem - hit an 000210 error when creating its first child file, and deleted the destination folder. What the heck could be doing this?