Simple Merge of Shapefiles in a single folder

16074
19
12-05-2012 09:00 AM
TylerJones2
New Contributor
Hello,

    I am very new to Python scripting and apparently not very good at it. I have a single folder with many shapefiles that I need to merge into a single shapefile. They are in the same projection and will not hit the 2gb limit so I assume this would be the easiest type of script to write. I have been trying to execute from PythonWin since that is the IDE the ESRI training uses but at this point I don't care where I execute it just that it works. I assume the workflow to be something like: import arcpy, set the environment to that particular folders location, apply overwrite to true, create a list of the shapefiles in the folder (i assume by telling it to look for all files with the ending ".shp"), the using the Merge_management to execute the merge.
    So with that said, could anyone offer some help. I have found a few other scripts online but they have some extra arguements for parsing and what not and I could not get them to work. All help appreciated
Tags (2)
0 Kudos
19 Replies
by Anonymous User
Not applicable
Try this:

import arcpy, os

out = r'Output\folder\location' # change this
arcpy.env.workspace = r'Some\Path\Name'  #change this
shplist =  arcpy.ListFeatureClasses('*.shp')
arcpy.Merge_management(shplist, os.path.join(out, 'Merged_fc.shp'))
print 'done'

DonnetteThayer
New Contributor

Old thread, but helped me in Arc 10.2 with very little tweaking. Thanks a million, Caleb Mackey.

TylerJones2
New Contributor
I'm getting an error for invalid input. This seems to be the same problem I have had with the other scripts. Can't find any extra info on the input parameters in the help section.
0 Kudos
by Anonymous User
Not applicable
Weird, this works for me.  The input parameters need to be supplied as a list (in square brackets []).  Can you add a print statement to show the list? Perhaps you can find the problem there.  Here is a script converting coverages to shapefiles and merging at the end.  I have a similar merge operation below:

import arcpy, os, sys, traceback
arcpy.env.overwriteOutput = True

# Workspace for interchange files
ws = r'C:\IGIC\Beginner\Unit_1\DATA\EffigyMounds\InterchangeFiles'
arcpy.env.workspace = ws

# Local Variables
out = r'C:\IGIC\Beginner\Unit_1\DATA\EffigyMounds\Fire'

try:
    # Creates a folder if it doesn't already exist
    if not os.path.exists(out):
        os.makedirs(out)

    # List all ArcInfo Interchange files (.e00)
    for cov in arcpy.ListFiles('*.e00'):
        arcpy.ImportFromE00_conversion(cov, ws, cov.split('.')[0])
        print 'Converted %s' %cov

    # Exclude Interchange files and info folder
    for cov in arcpy.ListFiles():
        if not '.e00' in cov and cov != 'info':
            arcpy.env.workspace = os.path.join(ws,cov)
            for fc in arcpy.ListFeatureClasses('*polygon'):
                arcpy.FeatureClassToFeatureClass_conversion(fc, out, '%s.shp' %cov)
                shp = os.path.join(out, '%s.shp' %cov)
                print 'Converted Coverage to %s' %shp

    # List shapefiles in folder to create input list for merge
    arcpy.env.workspace = out
    shplist =  arcpy.ListFeatureClasses()
    arcpy.Merge_management(shplist, os.path.join(out, 'All_Fires.shp'))
    print 'done'
    
except:
    
    # Get the traceback object
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + \
            "\nError Info:\n" + str(sys.exc_info()[1])
    msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
    arcpy.AddError(pymsg)
    arcpy.AddError(msgs)

    # Print Python error messages for use in Python / Python Window
    print pymsg + "\n"
    print msgs

0 Kudos
RDHarles
Occasional Contributor
This is the ultra simplified version.  Merge all fcs in the given directory into a shapefile called "Merged.shp"

import arcpy
arcpy.env.workspace = "c:/junk/mergeMe"
fcs = arcpy.ListFeatureClasses()
arcpy.Merge_management(fcs, "Merged.shp")
0 Kudos
MichaelVolz
Esteemed Contributor
Tyler:

What exactly is the error message that you are receiving?

Also, can you post the code that you are trying to run?
0 Kudos
TylerJones2
New Contributor
So this is what my code looks like (copied from the python window). I am sure I have some simple error but it is hard to fix when your still at the starting gate. Is there some way that I can actually see what the Listing looks like? As in have it create a simple .txt or something similar so I can see if the ListFeatureClasses command is even finding what it is supposed to find? I tried to simply "print Listing" but all I get are "[]" as a result which makes me think it is not seeing the file in the first place for some reason. This file has about 15 shapefiles (and their associated files) generated by the eCognition image processing software and I am using it for testing because I have much bigger ones with thousands of very small shapefiles that I am really gonna need Python for. Anyway, my error message below is talking about input parameters. Really wish they offered a python course here at my university. Again I appreciate the help from you guys.


import arcpy
>>> arcpy.env.workspace = "D:eCognition Results/Landcover/Buildings"
>>> Listing = arcpy.ListFeatureClasses()
>>> arcpy.Merge_management(Listing,"MergedFile.shp")
Runtime error <class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid. ERROR 000735: Input Datasets: Value is required Failed to execute (Merge). 
0 Kudos
by Anonymous User
Not applicable
It appears that you are getting an empty list.  I would double check your path to make sure it is correct.   Also, not sure if this is the issue, but I would try putting an underscore between Cognition and Results (Cognition_Results) for that folder.  ArcGIS does not like spaces in paths.  I would do that then use your print statement (your print statement is correct).
0 Kudos
MichaelVolz
Esteemed Contributor
Tyler:

It looks like this line might show the problem.

arcpy.env.workspace = "D:eCognition Results/Landcover/Buildings"

Should it read something like this instead:

arcpy.env.workspace = "D:/eCognition Results/Landcover/Buildings"

where you are currently missing a slash.
0 Kudos