1

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
reload(sys)
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))
 break
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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 20, 2020 at 7:15

1 Answer 1

2

I like collections.defaultdict(list):

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

Example:

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'):
 print(file)
 d[file[:2]].append(os.path.join(root, file))
answered Aug 20, 2020 at 7:58
5
  • I am quite new to python script. How can I set the output folder and shp file ( #and file.endswith('.shp'))? Commented Aug 20, 2020 at 8:20
  • I've changed the code Commented 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) Commented Aug 20, 2020 at 8:59
  • :) . Yes use os.walk like you tried instead of os.listdir Commented Aug 20, 2020 at 9:04
  • 1
    I did it perfectly ! THANK you for your super-kind answer ! Commented Aug 20, 2020 at 9:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.