I am trying to use my Python script for Merge in ArcGIS as a tool. (to use this script in model)
But it occurs an error (below figure).
I don't know what problem is...
Python script : work in ArcGIS geoprocessing-python
import sys, arcpy, os reload(sys) from collections import defaultdict as dd arcpy.env.workspace = r"C:\test2" folder = arcpy.env.workspace out_folder = r"C:\test1" sys.setdefaultencoding('utf-8') d = dd(list) for root, folder, files in os.walk(folder): for file in files: if os.path.isfile(os.path.join(root, file)) and file.endswith('.shp'): ## print(file) d[file[:4]].append(os.path.join(root, file)) #Execute code above then print(d) to make sure its ok before merging for group, mergelist in d.items(): #print(group, mergelist) arcpy.Merge_management(inputs=mergelist, output=os.path.join(out_folder, '{}.shp'.format(group)))
2. Revised Python script for the custom tool : error
import sys, os
reload(sys)
from collections import defaultdict as dd
import arcpy
from arcpy import env
inWorkspace = arcpy.GetParameterAsText(0)
outWorkspace = arcpy.GetParameterAsText(1)
env.workspace = inWorkspace
sys.setdefaultencoding('utf-8')
d = dd(list)
for root, inWorkspace, files in os.walk(inWorkspace):
for file in files:
if os.path.isfile(os.path.join(root, file)) and
file.endswith('.shp'):
## print(file)
d[file[:4]].append(os.path.join(root, file))
#Execute code above then print(d) to make sure its ok before merging
for group, mergelist in d.items():
#print(group, mergelist)
arcpy.Merge_management(inputs=mergelist,
output=os.path.join(outWorkspace, '{}0.shp'.format(group)))