Put Shapefile name into attribute table

12-03-2013 06:44 AM
New Contributor III
I iterate through a list of shapefiles and do some geoprocessing chains and field calculations for each of the input shapes (works fine).

The result is a dbf for each of the input shapefiles. Now I'd like to write the name of the input shapes into a field in the output dbf and then merge the dbfs. Merging works also fine. The naming process worked fine using ArcGIS 10 or 10.1, but fails using ArcGIS 10.2 and Python 2.7.3.

Some snippets:

#Blabla Environment + Import

#List FCs

name = []
x = 0
fcList = arcpy.ListFeatureClasses()

for fc in fcList:
    print fc
    name.insert(x, fc.split(".")[0])
    x += 1

#Blabla Geoprocessing


#Write Name into output dbf and merge
for fc in fcList:
        arcpy.AddField_management(input,"NAME", "TEXT")
        arcpy.CalculateField_management(input,"NAME", "name", "PYTHON")
        x += 1   

out = outws + "Merge" + ".dbf"
result = arcpy.Merge_management (input, out)

Printing x, the input and name returns the right values for x and the file names (e.g. B, C, D, E, F). But only the first name is written into the table, e.g.:

0 --------    .    ------B
1 --------    .    ------B
2 --------    .    ------B
3 --------    .    ------B
4 --------    .    ------B

Any idea? By the way, I get now error message, but the info "returned exit code 0".


Tags (2)
0 Kudos
3 Replies
Occasional Contributor III
Calculate field calculates the values for all rows, unless there is a selection on the layer... You possibly need an update cursor...?

I still don't entirely understand what you are doing, but maybe this will help:
arcpy.CalculateField_management(input,"NAME", "%s" % name, "PYTHON")

The difference is that name is a variable in your script, the CalculateField can't really access it in the normal way (or at all, which might be causing your error). The line above is simply setting the field to a string, and the string is defined within the script, so it will update with each x.

If this doesn't answer your question please post an explanation of your desired output versus what is actually coming out (you did this part last time, but I still don't know how that differs from what you are after).
0 Kudos
Occasional Contributor
I think it has to do with the string value of your formula for calculating the name of the feature.  Note the double quotes holding the name in single quotes by substituting string values in the third part of the arcpy.CalculateField_management() method.

import os
import arcpy

fieldNameToAdd = "NAME"

    shpDir = r"C:\ShapeFileDir"
    arcpy.env.workspace = shpDir

    fcList = arcpy.ListFeatureClasses()

    for fc in fcList:
        print "processing: %s" % (fc)
        fcName = fc.split(".")[0]
        fcPath = shpDir + os.sep + fc
        # blah geoprocessing ...
        fLength = len(fcName)
        # check if field "Name" already exists
        existingFields = arcpy.ListFields(fcPath)
        for field in existingFields:
            print field.name
            if field.name.lower() == fieldNameToAdd.lower():
                print "the field named %s already exists.  Try again or follow the commented instructions below." % (fieldNameToAdd)
                #raise # exception
                arcpy.DeleteField_management(fcPath, fieldNameToAdd)

        # blah continue working
        arcpy.AddField_management(fcPath,fieldNameToAdd, "TEXT", None, None, fLength)
        arcpy.CalculateField_management(fcPath,fieldNameToAdd, "'%s'" % (fcName), "PYTHON")
        # blah more geoprocessing...
    print "there was an error"
    print arcpy.GetMessages()
    #import traceback
0 Kudos
New Contributor III
Thank you both very much!

arcpy.CalculateField_management(input,"NAME", "'%s'" % (name), "PYTHON")
did the trick!
0 Kudos