I work on a project that is very repetitive and would like to have a python script for it. I am trying to export a feature class from SDE which contains an expression. The geodatabase has been created and I have the following code,
# Import system modules
from arcpy import env
# Set the workspace
arcpy.env.workspace = "Database Connections\ADMIN Edit.sde\GIS.DBO.SewerCollectionNetwork"
ArcSDE = arcpy.env.workspace
env.overwriteOutput = True
# Set local variables
inFeatures = ['sewer']
outputDatabaseLocation = "C:\Users\gis\Desktop\sewer\CCTV.gdb"
outputFeatureClass = "SewerMains"
delimitedField = arcpy.AddFieldDelimiters(arcpy.env.workspace, "OWNER")
expression = delimitedField + " = 'LOCAL'"
# Export feature class
print("Exporting feature class to CCTV geodatabase")
arcpy.FeatureClassToFeatureClass_conversion(inFeatures, outputDatabaseLocation, outputFeatureClass, expression)
I get the following error,
Traceback (most recent call last):
File "B:\Scripts\Python\ArcSDE Maintenance\CCTV Feature Class.py", line 25, in <module>
arcpy.FeatureClassToFeatureClass_conversion(inFeatures, outputDatabaseLocation, outputFeatureClass)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\conversion.py", line 1891, in FeatureClassToFeatureClass
RuntimeError: Object: Error in executing tool
I have tried using FeatureClassToGeodatabase and FeatureClassToShapefile with success, but I need to be able to add an expression to only export specific values from a field.
Any help will be appreciated.
Solved! Go to Solution.
Take a look at this document:
It mentions where and how to add an expression. Here is the example:
# Name: FeatureClassToFeatureClass_Example2.py # Description: Use FeatureClassToFeatureClass with an expression to create a subset # of the original feature class. # Import system modules import arcpy # Set environment settings arcpy.env.workspace = "C:/data/GreenvalleyDB.gdb/Public Buildings" # Set local variables inFeatures = "buildings_point" outLocation = "C:/output/output.gdb" outFeatureClass = "postoffices" delimitedField = arcpy.AddFieldDelimiters(arcpy.env.workspace, "NAME") expression = delimitedField + " = 'Post Office'" # Execute FeatureClassToFeatureClass arcpy.FeatureClassToFeatureClass_conversion(inFeatures, outLocation, outFeatureClass, expression)
Oh, I see that now. Sorry about that. If you were to not include the expression, does the script work ok? I guess I'm trying to figure out where the error is coming in at (maybe it is with the syntax of the expression)?
I am not sure. I have tried it without the expression and it does the same thing. I have double checked my variables to make sure everything is correct and still cannot find what I am doing wrong.
Are you sure this is correct? inFeatures should be a Feature Layer, not a list.
edit: you should also get in the habit of using forward slashes or raw string notation (e.g. r'path') for paths, otherwise you risk introducing unwanted escape characters.