Put Shapefile name into attribute table

1143
3
12-03-2013 06:44 AM
KathrinSchulte-Braucks
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
x=0
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.:


FID------BLA----NAME
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".

Thanks

Kathrin
Tags (2)
0 Kudos
3 Replies
StacyRendall1
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
MaxSquires
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.

solution:
import os
import arcpy

fieldNameToAdd = "NAME"

try:
    
    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
                #OR
                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...
        
except:
    print "there was an error"
    print arcpy.GetMessages()
    #import traceback
    #raise
    #etc
0 Kudos
KathrinSchulte-Braucks
New Contributor III
Thank you both very much!

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