以下代码是使用ArcToolBoxs工具运行的,如果需要直接在脚本中运行,修改接受参数方式即可。
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Created on: 2018-08-18
# (generated by GL:qq,695396984)
# Description:
# ---------------------------------------------------------------------------
#shp文件夹
workspace = arcpy.GetParameterAsText(0)
#输出文件夹
output_folder = arcpy.GetParameterAsText(1)
arcpy.env.workspace = workspace
Dict = {}
#"walk" through subfolders and below, and drop all contents into the empty dictionary
#unique shapefile names as keys, and with their file path as values.
for root, dirs, files in os.walk(workspace):
for dir in dirs:
arcpy.env.workspace = os.path.join(root,dir)
for fc in arcpy.ListFeatureClasses():
if not fc in Dict:
Dict[fc] = []
Dict[fc].append(os.path.join(root,dir,fc))
else:
Dict[fc].append(os.path.join(root,dir,fc))
#loop through each key and merge them together
for key in Dict:
output = os.path.join(output_folder,key[:-4]) + ''
arcpy.Merge_management(Dict[key], output)
print output + " created"