In this tutorial, we will be adding the attribute to the shapefile and updating its value using ArcPy. You need to have ArcGIS installed in order to use ArcPy. This script has been tested with ArcGIS Version 10.6 but should work with most versions.

Go ahead and download the data from here. This data has 3 shapefiles – Kathmandu, Bhaktapur, and Lalitpur. We want to add a new attribute called name and update its value with the shapefile name except without .shp, of course.

Here, the arcpy.env.workspace is where my shapefiles are located. arcpy.env.overwriteOutput with value of True will overwrite the existing shapefile.

import arcpy

# Set environment settings
arcpy.env.workspace = "E:/data/"
arcpy.env.overwriteOutput = True

    for infc in arcpy.ListFeatureClasses():
            arcpy.AddField_management(infc, 'name', 'TEXT', field_length=100)
            cursor = arcpy.UpdateCursor(infc)
            for row in cursor:
                value = infc.split('.shp')[0]
                row.setValue('name', value)
                print('done: {}'.format(infc))
        except Exception as e:
except arcpy.ExecuteError: