Tengo un script en el que estoy trabajando y me está dando un error en tiempo de ejecución. Estoy intentando crear una lista de todos los shapefiles dentro de un directorio raíz, luego agregar un campo a cada shapefile y calcularlo con el nombre del shapefile. Puedo generar la lista (convertida de un diccionario) pero cuando intento agregar el campo me encuentro con el 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()
La lista generada se ve así:
[['Q:\\\\GIS\\\\Field_Data\\\\MT\\023N052E\\Points.shp', 'Q:\\\\GIS\\\\Field_Data\\\\MT\\023N053E\\Points.shp', 'Q:\\\\GIS\\\\Field_Data\\\\MT\\024N052E\\Points.shp']]]
y el error:
File "P:\Scripts\scratch\scratch.py", line 45, in main
arcpy.AddField_management(fc, 'shpname','text')
: Object: Error in executing tool
Creo que tiene que ver con los cursores pero al ser novato no tengo idea de cómo resolver el problema
Gracias