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.


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))

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


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
  • Thanks for the example, your example is alot cleaner than mine. I used two for loops assuming that I had to loop through the fields list to get to "Desc". How do I post my code in the forum? I can't quiet figure it out. – Peter Wilson Feb 14 '13 at 22:41
  • To post code, see the Markdown Editing Help or use the icons along the top of the editor textbox: B I (hyperlink chain icon) " {} (image icon) followed by bullet formats. The {} can be used to format code blocks. – ccn Feb 14 '13 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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