AOI_test = "AOI_test.shp"
arcpy.env.overwriteOutput = True

for i in loc_list:
    AOI = "AOI_" + i + ".shp"
    arcpy.Select_analysis("locality", AOI, " \"LOCALITY\" = '%s'" % (i))
    arcpy.Merge_management([AOI_test, AOI], AOI_test)

This code isn't working for some reason. Have I understood this code incorrectly, or should I be able to overwrite the merge output FC (AOI_test) repeatedly, now that I have specified that this is acceptable?

  • 2
    What you describe superficially sounds like it should work, but the devil may be in the detail, so can you post a little more code to see if a reproducible test can emerge, please? – PolyGeo Aug 7 '13 at 2:21
  • possible duplicate of arcpy overwrite files in a geodatabase file – Fezter Aug 7 '13 at 3:13
  • @Fezter I don't think this one is a duplicate because that one uses file geodatabases and makes no mention of Merge while this one uses shapefiles and merge. I think this one is reproducible/explainable by my Answer whereas the other one remains non-reproducible. – PolyGeo Aug 7 '13 at 3:41
  • @PolyGeo, fair enough. Though, the reason I flagged it was a because of a comment by Baltok who mentioned schema locks could prevent overwriting. Using the merge tool in the way the OP describes is invalid because of a lock. – Fezter Aug 7 '13 at 3:50
  • Have you tried the upvoted suggestion on that question? – Devdatta Tengshe Aug 7 '13 at 7:19

I think the misunderstanding is with how Merge works, which is (with my bolding and italicizing), that it:

Combines multiple input datasets of the same data type into a single, new output dataset.

Let's assume that your loc_list is ["A","B"], then the first time Merge runs in your script the syntax used evaluates to:

arcpy.Merge_management(["AOI_test.shp", "AOI_A.shp"], "AOI_test.shp")

Your output dataset is NOT new, it is the same as one of your two inputs.

In response to our discussion as comments I have included the untested code below to illustrate how I am suggesting that you could perform a single Merge. As far as I can see there is no documented limit to the number of feature classes that can be merged together, but it will be interesting to see if 600 is too many. I would first test with 3 feature classes to make sure the code works first then see if any limitation is encountered when you retest with all 600*.

AOI_test = "AOI_test.shp"
arcpy.env.overwriteOutput = True
aoiList = []

for i in loc_list:
    AOI = "AOI_" + i + ".shp"
    arcpy.Select_analysis("locality", AOI, " \"LOCALITY\" = '%s'" % (i))

arcpy.Merge_management(aoiList, AOI_test)
  • I see. As advised by @Fezter on a separate, though entirely related question, 'append_management' would appear to be the correct tool to use in this instance as the intention is to append to an existing output as opposed to creating a new output every iteration. Thanks for your help. – TheLastGIS Aug 7 '13 at 3:41
  • Technically you could still use Merge to write to the same output each time - just so long as that output name was not the same as either/any of the inputs. Also, have you tried just making a list of all your inputs and processing them as a single Merge? I think that would work and be quicker. – PolyGeo Aug 7 '13 at 3:45
  • I cant conceptually work out how that would work. The merge I'm using is based on the output of a Select_analysis function. I assume the list would have to go in the Select_analysis function, though because that tool's selection process is based on SQL queries I would have to produce a list of 600+ localities in correct SQL list format, in order to select those localities from the existing shapefile "locality". I think this could end up being very messy. If I'm working this out correctly. – TheLastGIS Aug 7 '13 at 4:10
  • 1
    @JackHansen I've put some code into my updated Answer to show you what I was thinking. The very long where clause you mentioned might work but would be harder to build and more likely to hit some sort of line length limit I suspect. – PolyGeo Aug 7 '13 at 5:55
  • This still creates an individual shapefile for each locality. I think the append function will be suitable for my situation. Thanks for your greatly valued input. – TheLastGIS Aug 7 '13 at 6:36

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.