Solved! Go to Solution.
def main():     try:         import arcpy, sys, traceback, os, glob         arcpy.env.overwriteOutput = True         masterFolder = r"C:\tmp\Shp"         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 there's no dictioray key of shapefile name create one with empty list as value                 #... and append path to list                 if not shpName in shpDict:                     shpDict[shpName] = []                     shpDict[shpName].append(shpPath)                  #if there's a dictionary key of shapefile name just append path to the list                 else:                     shpDict[shpName].append(shpPath)           #Merge collected shapefiles into new shapefile         #for each dictionary key use its value (pathes list) as an input to Merge tool         for shapefile in shpDict:             outShp = os.path.join(outputFolder, shapefile[:-4]+"_merge.shp")             arcpy.Merge_management(shpDict[shapefile], outShp)             print outShp + " created."      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()def main(): try: import arcpy, sys, traceback, os arcpy.env.overwriteOutput = True masterFolder = r"C:\tmp\Shp" outputFolder = r"C:\tmp\Shp_merged" #collect list of subfolders subfolderLst = os.listdir(masterFolder) #collect all subfolder suffixes into list suffixLst = [] for subfolder in subfolderLst: suffixLst.append(subfolder[-1]) #for each suffix walk through all subfolders and collect shp names to merge for sfx in suffixLst: shpToMergeLst = [] #emtpy list to collect shps to merge for subfolder in subfolderLst: arcpy.env.workspace = os.path.join(masterFolder, subfolder) #build path to workspace wildcard = "*" + sfx +".shp" #build wildcard to restrict search in next step shapefilesLst = arcpy.ListFeatureClasses(wildcard) #one element list with shp name #add full shp path to list (if shp exists in this subfolder) if shapefilesLst: shpToMergeLst.append(os.path.join(masterFolder,subfolder,shapefilesLst[0])) #merge collected shps into new folder if shpToMergeLst: outShp = os.path.join(outputFolder, os.path.basename(shpToMergeLst[0][:-4])+"_merge.shp") arcpy.Merge_management(shpToMergeLst, outShp) print outShp + " created." 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()
Firstly I assume that 'filename' part of shapefile name is different in each subfolder (BTW, Merge tool wont allow for not unique names).