ArcGIS 10 SP4 Python 2.6.5

I have quite a few shapefiles that are in many different directories. I'd like to pull out groups of like shapefiles, merge them, and move the results into a FGDB. I've read the posts about making a GIS inventory, using a .csv as an input list, as well as few others. Those seem to point me in the right direction, but I'm still having a hard time wrapping my head around it. Obviously I'm new to Python, but believe automating this process is within my abilities.

It seems easy conceptually, but I don't have enough experience to generate the code.

Make multiple lists based on the shapefile name (startswith?) Use those lists as inputs for a batch merge. Convert merged files to GDB

  • @ccn: making some progress, but still not quite there. I ended up using the following, which is in the right direction, but now I need to use CreateUniqueName. – Michael Hamrick Apr 17 '13 at 18:47

See the similar post here how-to-merge-many-shapefiles-in-different-folders, but there are many, many ways to do this, so use what works best for your data.

The ArcGIS Desktop Help file on Listing data shows how you can use a wildcard to list feature classes in a workspace: arcpy.ListFeatureClasses(wildcard, feature_type). An added bonus is that you can also specify the feature_type; e.g. all points. You can ListWorkspaces for subdirectories and then loop through this list to generate your feature class list - you will need to change the arcpy.env.workspace each time, and make sure you concatenate the directory path prior to appending to the list.

Since you are starting out with shapefiles (and if all shapefiles with the wildcard name are of the same feature_type), I find the python glob module to be much faster and flexible for subdirectories:

import glob, os
wildcard  = '*insert_string_indicating_shapefile_group_name*' # use star on sides as needed
infolder = r'C:\Workspace\YourData' # change to your main working directory
dir = infolder + os.sep + '*' + os.sep + wildcard + '.shp'
flist = glob.glob(dir)

Once you have your list, simply insert into the arcpy.Merge_management(flist, output) tool and output directly to a FGDB.

  • Please correct me if I'm wrong. This is using the {flist = glob.glob(dir)} – Michael Hamrick Apr 5 '13 at 13:00
  • Thanks for the thoughtful response. Please correct me if I'm wrong. This is using the {flist = glob.glob(dir)} in place of arcpy.ListFeatureClasses to generate the list? Could you explain what the dir definition is doing? I don't understand the 'os.sep' variable. Thanks in advance, trying not to be a Help Vampire. – Michael Hamrick Apr 5 '13 at 13:14
  • Yes, glob.glob() is used in place of arcpy.ListFeatureClasses. The dir variable defines the input folders, which in this example may be C:\WorkSpace\YourData\subfolder1, C:\WorkSpace\YourData\subfolder2, C:\WorkSpace\YourData\subfolder3,... C:\WorkSpace\YourData\subfolderN, where the actual subfolder name is replaced by the '*' and the operating system separator - os.sep - is used instead of typing the slashes (which differs depending on your OS, which of course could be '/' or '\\' in Windows OS. – ccn Apr 5 '13 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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