Split Features by Attribute Python Script Error 000210 in ArcGIS

1061
7
Jump to solution
11-18-2017 03:42 PM
RebeccaLeach
New Contributor III

I'm working through an exercise for my Python class and my understanding/experience with Python is very limited.  In the end, once the script is built, I should come across one error that was purposely built into the script by the instructor, which I must fix, but when I run my script there seems to be more than one error.  And in all honesty, I have looked over the step by step guide and cannot see why I am getting these errors.  Any help would be greatly appreciated!

Here is the premise of the exercise:

Right click on the SplitFeaturebyAttribute.py python script that you created in part 1 and select Edit with Notepad++.
The next step is to add the actual logic that will execute the split and export of each unique feature class.
To do this the script will have to perform the following tasks:
1. Check to see if the destination geodatabase exits in the output directory.
2. Cursor through the input feature class to find the unique classes.
3. Split and export/create the unique feature classes.
To do this we will write a function that takes in the script tool input parameters (e.g. inputFc, outputDir, and geodatabaseName). When working with data and workspace paths a common python module to use is os (click os to view its library page). For the first task listed above we will have to first check to see if the output directory geodatabase exist or not. If it exists, then the next step will be to cursor through the feature class. If the geodatabase does not exist, then the script will create a new geodatabase. This logic may be represented using an if/else conditional logic statement. We will use a os function called os.path.exists to check to see if the geodatabase exists or not.

Below is the error statement I keep getting:

Executing: SplitFeaturebyAttribute "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13\test.shp" "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13" test
Start Time: Sat Nov 18 18:27:55 2017
Running script SplitFeaturebyAttribute...
*Geodatabase exists test in C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13
Traceback (most recent call last):
  File "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\SplitFeaturesbyAttribute.py", line 51, in <module>
    splitbyAttribute(inputFc, outputDir, 'CLASS')
  File "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\SplitFeaturesbyAttribute.py", line 48, in splitbyAttribute
    arcpy.Select_analysis(fc, newfc, field + '= ' + "'" + value + "'")
  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\analysis.py", line 84, in Select
    raise e
ExecuteError: ERROR 000210: Cannot create output C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13//test.gdb//Class1
Failed to execute (Select).
ERROR 000210: Cannot create output C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13//test.gdb//Class1
Failed to execute (Select).
Completed script SplitFeaturebyAttribute...
Failed to execute (SplitFeaturebyAttribute).
Failed at Sat Nov 18 18:27:55 2017 (Elapsed Time: 0.05 seconds)

Below is my script:

"""
---------------------------------------------------------------------------------------------
SplitFeatureAttribute.py
Author: Kevin Surbella
Organization: CSCC
Date: 11/10/2013
Description: This script splits a feature class by
               defined field unique classes.
---------------------------------------------------------------------------------------------
"""


import os, arcpy

inputFc = arcpy.GetParameterAsText(0)
outputDir = arcpy.GetParameterAsText(1)
geoDatabaseName = arcpy.GetParameterAsText(2)



try:

#check to see if geodatabase exists. Note geodatabase is a workspace.

     if not os.path.exists(outputDir):
          arcpy.CreateFileGDB_management(outputDir, geoDatabaseName, 'CURRENT')
     else:
          arcpy.AddMessage('\n*Geodatabase exists ' + geoDatabaseName + ' in ' + outputDir)
          
     arcpy.env.overwriteOutput = True
     
     def splitbyAttribute(fc, outDir, field):
     
          valueList = []
          valueSet = set()
          
          with arcpy.da.SearchCursor(fc, field) as cursor:
               for row in cursor:
                    if row[0]:
                    
                         if row[0] not in valueSet:
                              valueList.append(row[0])
                         
                         valueSet.add(row[0])
               
               for value in valueList:
                    newfc = outDir + '//' + geoDatabaseName + '.gdb//' + value
                    
                    arcpy.Select_analysis(fc, newfc, field + '= ' + "'" + value + "'")
                    arcpy.AddMessage('\nfeature exported = ' + newfc + '\n')
                         
     splitbyAttribute(inputFc, outputDir, 'CLASS')

except Exception, e:
  import traceback
  map(arcpy.AddError, traceback.format_exc().split("\n"))
  arcpy.AddError(str(e))
0 Kudos
1 Solution

Accepted Solutions
RebeccaLeach
New Contributor III

I just thought I had to truly only change the arcpy.Select_analysis.  That the folder paths didn't necessarily need to be changed, that you were just pointing out how those should be in the future.  I didn't think they were the actual cause of my errors.  I did go in and change them this time.  Still got the same error codes.  I relooked over my entire script and found that line 24 was incomplete.

if not os.path.exists(outputDir):

It should have been:

if not os.path.exists(outputDir + '//' + geoDatabaseName + '.gdb'):

After that was corrected, everything ran as it should have per the exercise requirements.  Thanks again for your input.  I'm learning the hard way how one little mistake can cause an entire script to fail.

View solution in original post

0 Kudos
7 Replies
DanPatterson_Retired
MVP Esteemed Contributor
arcpy.Select_analysis(fc, newfc, '"{}" = \'{}\''.format(field, value))

can you try that, I don't have anything to test on

RebeccaLeach
New Contributor III

I tried what you suggested above and got more error codes this time.  See below:

Executing: SplitFeaturebyAttribute "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13\test.shp" "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13" test
Start Time: Sun Nov 19 11:14:00 2017
Running script SplitFeaturebyAttribute...
*Geodatabase exists test in C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13
Traceback (most recent call last):
  File "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\SplitFeaturesbyAttribute.py", line 51, in <module>
    splitbyAttribute(inputFc, outputDir, 'CLASS')
  File "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\SplitFeaturesbyAttribute.py", line 48, in splitbyAttribute
    arcpy.Select_analysis(fc, newfc, '"{}" = \'{}\''.format(field, value))
  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\analysis.py", line 84, in Select
    raise e
ExecuteError: ERROR 000210: Cannot create output C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13//test.gdb//Class1
Failed to execute (Select).
ERROR 000210: Cannot create output C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13//test.gdb//Class1
Failed to execute (Select).
Completed script SplitFeaturebyAttribute...
Failed to execute (SplitFeaturebyAttribute).
Failed at Sun Nov 19 11:14:00 2017 (Elapsed Time: 0.04 seconds)
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

And your path construction can use a few tricks...

gdb_name = 'test.gdb'

value = 'Class1'

# ----TIP ---- don't use spaces!!! in folders or begin folders with numbers

a = r'C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13' 

out = "{}\\{}\\{}".format(a, gdb_name, value)

out
'C:\\CSCC\\Courses\\GIS 2120\\Unit 11\\Unit11\\Part2\\10_13_13\\test.gdb\\Class1'
RebeccaLeach
New Contributor III

And thanks for the tip in regards to folders.  

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

the paths are still the same... you didn't move anything nor did you alter the paths.  Are you sure you followed my suggestions?  I just was showing how to alter the paths to be compliant, I didn't fix everything.  

move the test.gdb and change the output names.  In your script you also need to add

arcpy.env.overwriteOutput = True  and at the system level

http://pro.arcgis.com/en/pro-app/help/analysis/geoprocessing/basics/geoprocessing-options.htm

C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13//test.gdb//Class1

C:\temp\test.gdb//Class01

RebeccaLeach
New Contributor III

I just thought I had to truly only change the arcpy.Select_analysis.  That the folder paths didn't necessarily need to be changed, that you were just pointing out how those should be in the future.  I didn't think they were the actual cause of my errors.  I did go in and change them this time.  Still got the same error codes.  I relooked over my entire script and found that line 24 was incomplete.

if not os.path.exists(outputDir):

It should have been:

if not os.path.exists(outputDir + '//' + geoDatabaseName + '.gdb'):

After that was corrected, everything ran as it should have per the exercise requirements.  Thanks again for your input.  I'm learning the hard way how one little mistake can cause an entire script to fail.

View solution in original post

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Good Rebecca.  It is a good idea to mark ... Correct ... the response that answered your question.  People then know that a solution was found when searching.