I am trying to edit the length of all the text fields in a feature class. The feature class has a lot of fields so I want to do this in python. I only have access to v10.1 so cannot use the AlterField_management option.
The way I'm going about it is to use fc to fc conversion with an appropriate field mapping.
Below is my code but it does not alter the length of the text fields as intended. The fc to fc conversion goes ahead but with no changes to any of the field properties. I'm pretty sure the problem is with the field mapping but I can't figure it out.
import arcpy
infc = r"C:\path to original fc"
out_space = r"C:\path to out gdb"
out_fc = "fc_newfieldlengths"
# set up field mappings object
fms = arcpy.FieldMappings()
# Create list of all non-string fields to skip when creating the fieldmaps
text_fields = arcpy.ListFields(infc, field_type = 'String')
all_fields = arcpy.ListFields(infc)
skip_fields = [x for x in all_fields if x not in text_fields]
# create field mapping with new length for the text fields
for field in all_fields:
if field in skip_fields:
pass
else:
fm = arcpy.FieldMap()
fm.addInputField(infc, field.name)
outfield = fm.outputField
outfield.length = 150
outfield.name = field.name
fm.outputField = outfield
fms.addFieldMap(fm)
# Copy featureclass with new field mapping
arcpy.FeatureClassToFeatureClass_conversion(infc, out_space, out_fc, field_mapping = fms)
# End of script
If I tried only looping through a list of string fields to generate the fieldmaps I found that all the fields were changed to string fields in the output, not just the fields I am interested in amending.
To avoid changing the data type for all the fields I created a skip list of fields not to alter - this manages to preserve the datatypes but has not amended the text field lengths (as was occurring previously but just also annoyingly changing all the datatypes as well).
I suspect I just don't understand field mappings well enough to know what I'm doing wrong.