Copy feature class for selected field

08-02-2012 05:58 AM
New Contributor

I have an existing code to copy feature class from sde to file geodatabase (using arcpy.FeatureClassToGeodatabase_conversion).     Recently I have added a new field  feature classes in sde. My requirement is new field should not come to file geodatabase. Is it possible to copy a featureclass from sde to file geodatabase having selected fields only?

Thanks in advance
Tags (2)
0 Kudos
3 Replies
Regular Contributor
if you first use arcpy.MakeFeatureLayer to reference the data you want to copy to the gdb then you can do this by making use of the last optional argument called 'field info'.. the help for this tool says this:
A subset of fields can be made unavailable in the new layer by using the Field Info control's visible property. The third column in the control provides a dropdown option to specify whether a field will be visible or hidden in the new layer. The default is TRUE. Selecting FALSE will hide that field. You cannot use the hidden fields in a workflow if the newly created layer is input to a subsequent process or tool.   If the output is saved to disk, only the fields listed as visible will appear in the new data.

Depending on how many fields are involved, setting up the fields data in a digestible fashion into the 'field info' part of this tool can be very cumbersome. A work around for this is to set up a dummy modelbuilder tool to do this for you, manage the fields through the GUI then export the model to a script and then copy/past the MakeFeatureLayer command and arguments into your script. A bit of a kludge but given my typing skills this works fairly well.

I'm not sure if the tool you are using accepts a feature layer as an input or wants a reference to a file on disk so that might alter your workflow a little. Is there a reason you are using the FeatureClassToGeodatabase_conversion tool rather than CopyFeatures?
0 Kudos
Esri Esteemed Contributor
You can also you the Feature Class to Feature Class (conversion) tool.  This allows you to use the field map option:

You can add, rename, or delete output fields as well as set properties such as data type and merge rule.
0 Kudos
New Contributor II

I found this example and made it even simpler. FC equals your feature class, fieldList equals what fields you want to keep. My code is below.

#Define function to take an input layer and output a layer with only a defined field list
def filter_fields(FC, fieldList):

# List input fields
fields= arcpy.ListFields(FC)

# Create a fieldinfo objects
fieldinfo = arcpy.FieldInfo()

# Iterate over input fields, add them to the FieldInfo and hide them if
# they aren't in the list of fields to be kept
for field in fields:
if not in fieldList:
fieldinfo.addField(,, "HIDDEN", "")

# Copy features to a layer using the FieldInfo
temp = "temp"
arcpy.MakeFeatureLayer_management(FC, temp, "", "", fieldinfo)

#Call the function
outFeature= "admin3_AllJoined"


0 Kudos