I've got a featureclass (fc) that I want to copy, but only retaining a selected number of fields, let's say field 9, 11, and 12 from the total of 15 fields. I want to use arcpy and suspect that fieldmappings are somehow required for this. I currently have:

fm= arcpy.FieldMappings()
sel= range(0,9)+[10]+range(13,16)
for i in sel:

However, this gives:

RuntimeError: FieldMappings: Error in removing a field map from field mappings for RemoveFieldMap

I can't quite see what I'm doing wrong? Perhaps it would be nicer to somehow select the fields I want and add these to a new fieldMapping object? All suggestions are welcome, thanks in advance!

Update. As suggested below, I now have the following:

inFields= ["fA", "fB", "fC"]
inTab= 'testFc'
fm= arcpy.fieldMappings()
fieldMap= arcpy.FieldMap()
for field in inFields: 
    fieldMap.addInputField(inTab, field)
arcpy.featureClassToFeatureClass_conversion(testFc, outDir, testOutFc, field_mapping= fm)

This works, a new featureclass is created. However, it contains only field fA. The python output reads:

Executing: FeatureClassToFeatureClass L:/GEOProjects/2Working.gdb/testFc :/GEOProjecten outDir testOutFc
# "fA "fA" true true false 8 Double 0 0 ,First,#,
L:/GEOProjects/2Working.gdb/testFc,fC,-1,-1" #
Start Time: Thu Oct 24 10:21:42 2013
Succeeded at Thu Oct 24 10:21:58 2013 (Elapsed Time: 16.00 seconds)

It seems that ony fA is being copied to the new featureclass.

Any suggestion as to why this can be?

  • 1
    When I try the above code, I get the following error: > Traceback (most recent call last): File "<pyshell#58>", line 1, in > module fieldMappings.addFieldMap(vars()[field]) NameError: name > 'fieldMappings' is not defined Indeed, what is the fieldMappings object? Jan 2, 2022 at 15:53
  • This may help (ArcGIS Pro): pro.arcgis.com/en/pro-app/latest/arcpy/classes/…
    – Pfalbaum
    Jan 3, 2022 at 19:29

1 Answer 1


Note: I've edited this answer to correct a mistake highlighted above

The problem is you are removing the field map to the table, and not removing the fields from the field map. So, it removes the first (and only) fieldmap, then it doesn't know what to do when you tell it to look for the next fieldmap.

What you need to do is create a new fieldmap and add the fields you want into it. Then, add it to your fm object. It would go something like this:

inTab = "InputTableName"
inFields = ['List','Of''Field','Names'] #case-sensitive

fm = arcpy.FieldMappings()
fieldMap = arcpy.FieldMap()
for field in fieldList:

   #creates a fieldmap with name of input field
   vars()[field] = arcpy.FieldMap()


Bear in mind, that code may need some adjusting, but it should get you in the right direction.


Your Answer

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

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