AnsweredAssumed Answered

Help needed: Python looping

Question asked by aaron.sidder on Oct 6, 2014
Latest reply on Oct 6, 2014 by Dan_Patterson



I'll try to give a brief background here.  I recently received a large amount of data that was all digitized from paper maps.  Each map was saved as an individual file that contains a number of records (polygons mostly).  My goal is to merge all of these files into one shapefile or geodatabase, which is an easy enough task.  However, other than spatial information, the records in the file do not have any distinguishing information so I would like to add a field and populate it with the original file name to track its provenance.  For example, in the file "505_dmg.shp" I would like each record to have a "505_dmg" id in a column in the attribute table labeled "map_name".  I am trying to automate this using Python and feel like I am very close.  Here is the code I'm using:


# Import system module

import arcpy

from arcpy import env

from import *


# Set overwrite on/off

arcpy.env.overwriteOutput = "TRUE"


# Define workspace

mywspace = "K:/Research/DATA/ADS_data/Historic/R2_ADS_Historical_Maps/Digitized Data/Arapahoe/test"

print mywspace


# Set the workspace for the ListFeatureClass function

arcpy.env.workspace = mywspace



    for shp in arcpy.ListFeatureClasses("","POLYGON",""):

            print shp

            map_name = shp[0:-4]

            print map_name

            arcpy.AddField_management(shp, "map_name", "TEXT","","","20")

            arcpy.CalculateField_management(shp, "map_name","map_name", "PYTHON")



    print "Fubar, It's not working"

    print arcpy.GetMessages()


    print "You're a genius Aaron"


The output I receive from running this script:


K:/Research/DATA/ADS_data/Historic/R2_ADS_Historical_Maps/Digitized Data/Arapahoe/test





You're a genius Aaron


Appears successful, right?  Well, it has been: a field was added and populated, and it is perfect for 505_dmg.shp.  Problem is. 506_dmg.shp is also been labeled "505_dmg" in the "map_name" column.  Though the loop appears to be working, the map_name variable does not seem to be updating.  Any thoughts or suggestions much appreciated.  Files attached in the zip file.