0

I have users creating data (shp) daily into a specific folder, I already know how to invoke GP tools to append these data sets into one. What I am trying to do is run a task (which I already know) to look into this folder and take shapefiles and append them together (same schema) into one dataset in another location.

If I have a folder (call it date) 042115 with 5 shapes inside 1,2,3,4,5.shp I want python to look into the folder and take these 5 shapefiles and append them together to another location. I know how to do this with the GP tools, but each day the files names change and I have to continually tweak the model to accomodate the changed file names. What I was hoping for was a python script that would take these (at a folder level) and start the operation without worrying about the specific names. I hope this makes sense, at a lower level I am trying to invoke an append.management operation on shp within a folder and export to another dataset.

8
  • 1
    For starters you can use arcpy.da.Walk to look through folders for the shapefiles you wish to append. Walk through the folder and create a list of all the shapefiles in it. Loop through this list of shapefiles and use them as input into the desired GP tool (e.g. append).
    – JKyllo
    Commented Apr 21, 2015 at 21:11
  • Ok this helps. Would you have some sample code to get me up and running? I know it is alot to ask, but I am quite new to python. Thanks
    – Robert C
    Commented Apr 21, 2015 at 21:18
  • Does each folder have a unique ID attached to the file (e.g. Folder1: 0421115_shapefile_1.shp, 0421115_shapefile_2.shp,...)? How do you want to define the output names?
    – Aaron
    Commented Apr 21, 2015 at 21:19
  • Yes, each folder would be the date so for today 042115 (then inside it would be a line and a point feature like JobA_12345.shp (point) and JobB_12345.shp (line)). The output would be static dataset (like AllJobs.shp).
    – Robert C
    Commented Apr 21, 2015 at 21:26
  • 1
    So are you appending all those shapefiles to a master shapefile or merging all the new shapefiles into a new single shapefile?
    – JKyllo
    Commented Apr 21, 2015 at 21:43

1 Answer 1

3

Here is a block of code to get you started. This should change dependent on whether you are performing an append or merge. You will also have to create separate lists and loops/appends for your line data and point data.

# import modules to use
import arcpy, os

# input folder containing your new shapefiles
inputFolder = arcpy.GetParameterAsText(0)

# shapefile to append to
masterShapefile = arcpy.GetParameterAsText(1)

# create list to contain shapefile paths
shapefiles = []

# walk through folder and only return feature classes (which will return shapefiles)
# we are performing appends so we want to limit the type of feature class returned - in my case polygons
for dirpath, dirnames, filenames in arcpy.da.Walk(inputFolder,
                                                  datatype="FeatureClass",
                                                type="Polygon"):
    # append shapefile path to list
    for filename in filenames:
        shapefiles.append(os.path.join(dirpath, filename))

# append shapefiles to master shapefile
for shapefile in shapefiles:
    arcpy.Append_management(shapefile, masterShapefile, "TEST")
2
  • Yes, that should work well. Because the OP only mentioned working in one directory, you could also use os.listdir() and only pull files that end in ".shp"
    – mr.adam
    Commented Apr 21, 2015 at 21:56
  • Like it. Also arcpy.ListFeatureClasses(".shp")... the one thing you need to address is that shapefiles *can be different geometry types.. have a look at the answer gis.stackexchange.com/questions/109075/… about splitting them out to points, lines and polygons. Commented Apr 21, 2015 at 22:24

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