Solved! Go to Solution.
The folder names are completely independent to the file names. Unfortunately. Every subfolder within the master folder needs to be chchecked for the repeating file names.
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()
for shapefile in shpDict: outFC = os.path.join(outputGDB, shapefile[:-4]+"_merge") arcpy.Merge_management(shpDict[shapefile], outFC) print outFC + " created."