Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any phyton/VB script which will add a field to shapefile which will be corresponding to first three characters from the shapefile name ?

I am working in modelbuilder and arcgis 10.

Thanks in advance.

share|improve this question

you can do this with Add Field (Data Management).


AddIndex_management (in_table, fields, {index_name}, {unique}, {ascending})

import arcpy
from arcpy import env

env.workspace = "C:/test.gdb"

inFeat = "cityoflondon.shp"
fieldLength = 10

#it will return "cit" from inFeat
fieldName = str(inFeat[:3])

arcpy.AddField_management(inFeat, fieldName, "TEXT", "", "", fieldLength)

i hope it helps you...

share|improve this answer
I forgot to add that i need to add field in multiple shapefiles at once. I have one hundred shapefiles. Sorry, my mistake! – com May 11 '12 at 8:08

This code should do the trick. New field should be titled "New"

import arcpy
arcpy.env.workspace = r"c:\Temp"
shapename = "newshape.shp"
newfield = shapename[0:3]
share|improve this answer
shapename[0:3] would give the first 4 letters, no? – Roy May 10 '12 at 16:51
@Roy, This notation starts at and includes the first number, and ends at, but excludes, the last number. – Get Spatial May 10 '12 at 16:54
Thanks, indexing always gets me. Thank god for trial and error. – Roy May 10 '12 at 17:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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