I have a script I am working on and am hitting a runtime error. I am attempting to create a list of all of the shapefiles within a root directory, then add a field to each shapefile and calculate it with the shapefile name. I am able to generate the list (converted from a dictionary) but when I attempt to add the field I encounter the error.
def main():
try:
import arcpy, sys, traceback, os, glob
arcpy.env.overwriteOutput = True
masterFolder = r"Q:\\GIS\\Field_Data\\MT"
outputFolder = r"C:\tmp\Shp_merged"
#collect a list of subfolders in master folder
subfolderLst = os.listdir(masterFolder)
#declare a dictionary where a key will be shapefile name
#... and value a list of pathes to shapefile with this name in all subfolders
shpDict = {}
#loop through all subfolders
for subfolder in subfolderLst:
#check current subfolder and make a list of pathes to each .shp file
shpLst = glob.glob(os.path.join(masterFolder,subfolder,'*.shp'))
#add each shapefile path to dictionary
for shpPath in shpLst:
shpName = os.path.basename(shpPath)
if not shpName in shpDict:
shpDict[shpName] = []
shpDict[shpName].append(shpPath)
else:
shpDict[shpName].append(shpPath)
shpDict = shpDict.values()
print shpDict
for fc in shpDict:
arcpy.AddField_management(fc, 'shpname','text')
arcpy.CalculateField_management(fc, 'shpname', '"'+fc+'"'+ time.strftime('%m_%d_%y'))
#arcpy.Merge_management(fcs, 'out.shp')
except:
print arcpy.GetMessages()
# Get the traceback object '"' + wildcard + '"'
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
# Concatenate information together concerning the error into a
# message string
pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)
# Return python error messages for use with a script tool
arcpy.AddError(pymsg)
# Print Python error messages for use in Python/PythonWin
print pymsg
if __name__ == '__main__':
main()
The list generated looks like this:
[['Q:\\\\GIS\\\\Field_Data\\\\MT\\023N052E\\Points.shp', 'Q:\\\\GIS\\\\Field_Data\\\\MT\\023N053E\\Points.shp', 'Q:\\\\GIS\\\\Field_Data\\\\MT\\024N052E\\Points.shp']]]
and the error:
File "P:\Scripts\scratch\scratch.py", line 45, in main
arcpy.AddField_management(fc, 'shpname','text')
<type 'exceptions.RuntimeError'>: Object: Error in executing tool
I think it has to do with cursors but being a novice I have no idea how to resolve the problem
Thanks

