[GIS] ArcGIS ArcPy: Iterate over shapefiles and report field “Desc” Length

arcpy

I've just started learning to script using python, i.e. no programming experience. I need to iterate over a folder that contains multiple shapefiles that all start with nlc… I need to iterate over each shapefile and then report the length of the field "Desc". The reason that I need to do this is that the field lengths are not the same and I've generated a model in modelbuilder that iterates over the folder, generates a feature layer (in_memory) filtering the output layer by using a sql expression to return a specified set of records and then append the output records into a single feature class. I only realised half way through running my model that the values in certain shapefiles are larger than the initial field length that I set for the new feature class.

An example to get me started, would really be appreciated, thanks in advance.

Peter

The following is the initial python script that I wrote, not realising that I didn't need to loop through the fields list:

    import arcpy
... arcpy.env.workspace = r'E:\Projects\H109009\data_received\DOA\NLC2000\tiles'
... fcs = arcpy.ListFeatureClasses("nlc*","polygon")
... for fc in fcs:
...     fields = arcpy.ListFields(fc,"Desc*")
...     for field in fields:
...         print("{0} is a type of {1} with a length of {2}"
...           .format(field.name, field.type, field.length))

Best Answer

Listing data has code samples for accessing the shapefiles (ListFeatureClasses(wild_card, feature_type)) and fields (ListFields(dataset, wild_card, field_type)) by name.

Example:

import arcpy
from arcpy import env
env.workspace = r'C:\your\directory'
fcs = arcpy.ListFeatureClasses('nlc*')
for fc in fcs:
    fld_list = arcpy.ListFields(fc, 'Desc')
    fld_len = fld_list[0].length
    print fld_len
Related Question