I am trying to merge files starting with same 2 characters as below;

  1. 11111 - merge (output name : 11.shp)
  2. 11112 - merge (output name : 11.shp)
  3. 11113 - merge (output name : 11.shp)
  4. 22112 - merge (output name : 22.shp)
  5. 22153 - merge (output name : 22.shp)

But my python script' result merge together if "11" is including in the filenames(like 4)). I only want 11***.shp(1)~3)) but A Script merges 11.shp (merge 1~4 as 11.shp)


import sys, os, arcpy, fnmatch

arcpy.env.workspace  = r"F:\a"

workspace = arcpy.env.workspace
outdir = r"F:\b"

List = []

for dirpath, dirnames, filenames in os.walk(workspace, topdown=True):
    for filename in filenames:
        List.append(os.path.join(dirpath, filename))

patterns = set([os.path.basename(fl)[:2] for fl in List])

for pattern in patterns:
    group = [fl for fl in List if fnmatch.fnmatchcase(fl, '*' + pattern + '*.shp')]
    output = os.path.join(outdir, pattern)
    arcpy.Merge_management(group, output)


How can I fix it?

1 Answer 1


I like collections.defaultdict(list):

...to group a sequence of key-value pairs into a dictionary of lists


import arcpy, os
from collections import defaultdict as dd

folder = r'C:\GIS\data\testdata\merge'
out_folder = r'C:\GIS\data\testdata\outfolder'

d = dd(list)

for item in os.listdir(folder):
    if os.path.isfile(os.path.join(folder,item)) and file.endswith('.shp'):
        d[item[:2]].append(os.path.join(folder, item))

#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, 'Merge_{}.shp'.format(group)))

If you want to search subdirectories aswell use os.walk like you tried, example:

import os
from collections import defaultdict as dd

folder_main = r'C:\GIS\data\testdata'
d = dd(list)
for root, folder, files in os.walk(folder_main):
    for file in files:
        if os.path.isfile(os.path.join(root, file)) and file.endswith('.shp'):
            d[file[:2]].append(os.path.join(root, file))
  • I am quite new to python script. How can I set the output folder and shp file ( #and file.endswith('.shp'))?
    – user127580
    Aug 20, 2020 at 8:20
  • I've changed the code
    – BERA
    Aug 20, 2020 at 8:29
  • 1
    You are briliant !! Last one more question please,,, If input folder has sub-directories, can it be include as input (Above script seems sub-dir files excluded)
    – user127580
    Aug 20, 2020 at 8:59
  • :) . Yes use os.walk like you tried instead of os.listdir
    – BERA
    Aug 20, 2020 at 9:04
  • 1
    I did it perfectly ! THANK you for your super-kind answer !
    – user127580
    Aug 20, 2020 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.