# Using erase tool in Python?

I am trying to create a python scrip to batch erase shapefiles. I have created a script that works, however, I know there is a better way and I just am not good enough at scripting to figure it out. I want to create a loop to allow the script to just to go through the folders and just erase the new shapefile from the old to see change. The way I have it now I have to update about 16 path names. Also, the the script will send me an error if there is nothing to erase. I know this requires an if statement but again, I am just now knowledgeable enough to figure it out. Below, is the scrip that works and hopefully it will give and idea of what it is I am trying to achieve.

import arcpy
#Set workspace
arcpy.env.workspace = "USER_TEMP\\RAY\\test\\"

#Erase workspace
erase_ADE01 = "\\ADE01_ALL_BUFFERS_05_09_16.shp"
erase_COF01 = "\\COF01_ALL_BUFFERS_05_09_16.shp"
erase_CVI04 = "\\CVI04_ALL_BUFFERS_05_09_16.shp"

#Current buffers workspace
current_ADE01 = "\\ADE01_ALL_BUFFERS_TEST.shp"
current_COF01 = "\\COF01_ALL_BUFFERS_TEST.shp"
current_CVI04 = "\\CVI04_ALL_BUFFERS_TEST.shp"

#Outputworkspace
output = "\\test\\delta_test\\"

#Erase tool; NEED HELP-WHEN THERE IS NOTHING TO ERASE SCRIPT FAILS
arcpy.ET_GPErase(current_ADE01, erase_ADE01, output + "ADE01_ALL_DELTAS_TEST.shp", 0.0)
arcpy.ET_GPErase(current_COF01, erase_COF01, output + "COF01_ALL_DELTAS_TEST.shp", 0.0)
arcpy.ET_GPErase(current_CVI04, erase_CVI04, output + "CVI04_ALL_DELTAS_TEST.shp", 0.0)

print "Script completed"


UPDATE:

So, I don't know why I didn't think of this before but what I did was merge the erase features together so that way there is only one feature to erase from. Then I just threw it in a "for loop" and everything works well for that part. Now I am still getting that error for when there is nothing to erase (the features are the same). How would I write an if statement for this:

#Erase tool; NEED HEELP-WHEN THERE IS NOTHING TO ERASE SCRIPT FAILS
for fc in arcpy.ListFeatureClasses():
erase_feature =  "\\all_erase.shp" #this is the merged feature
erase_output = "\\test\\delta_test\\" + "_ALL_DELTAS_TEST.shp"
arcpy.ET_GPErase(fc, erase_feature, erase_output, 0.0)


UPDATE2:

So, I am getting closer to figuring this out. My script will run all the way to the end now. However, it wasn't erasing because of memory issues. So i upgraded to python64x. Here is my script:

for fc in arcpy.ListFeatureClasses():
prefix = fc[:6]
erase_output = "\\test\\delta_test\\" + prefix + "_ALL_DELTAS_TEST.shp"
try:
arcpy.ET_GPErase(fc, merge_output, erase_output, 0.0)
except arcpy.ExecuteError:
print arcpy.GetMessages(2)
print "Erase for " + prefix + " unsuccessful"
except:
print "There has been a non-tool error"


The "There has been a non-tool error" print pops up now that I have downloaded the different pywin. Before I did that, the error was in the first except because of the memory issues. What am I doing wrong?

• Before GPErase, issue a Select by Location with Intersect, to test for Overlap. If get_count is >0 then run Erase. – klewis May 20 '16 at 17:21
• That is a great idea. The issue is however, that this is after I have dissolved the buffers so there is only one attribute in the table so everything gets selected. – Ray Braden May 20 '16 at 17:39
• Instead of posting a screenshot of code, please edit your post to include the code. There's also two separate issues you're trying to solve, so tackle the first before attempting the second. – Paul May 20 '16 at 18:30
• I was able to edit it, that is definitely better than a screenshot. I want to figure out the loop that can pull from both files compare and erase the proper shapefiles first and for most. Then, that if statement for the error of nothing to erase would be next. I have been reading through my old textbooks to try and help me with these loops and everything that I try does not seem to work. – Ray Braden May 20 '16 at 19:09
• What is the ET_GPErase tool that you are using? At the moment your code snippet is more than minimal. For example 2-3 erases is enough to illustrate a loop. – PolyGeo May 20 '16 at 19:39

## 1 Answer

To avoid reapeating the ET_GPErase tool you can group all of your inputs and outputs in a list, and then use a loop. Here's how that could be achieved:

import arcpy
#Set workspace
arcpy.env.workspace = "USER_TEMP\\RAY\\test\\"

#Erase and Current buffer workspace
eraseAndCurrent = [
["/ADE01_ALL_BUFFERS_05_09_16.shp","/ADE01_ALL_BUFFERS_TEST.shp"],
["/COF01_ALL_BUFFERS_05_09_16.shp","/COF01_ALL_BUFFERS_TEST.shp"],
["/CVI04_ALL_BUFFERS_05_09_16.shp", "/CVI04_ALL_BUFFERS_TEST.shp"]
]

#Outputworkspace
output = "/test/delta_test"

#Erase tool; NEED HELP-WHEN THERE IS NOTHING TO ERASE SCRIPT FAILS
for item in eraseAndCurrent:
erase = item[0]
current = item[1]
prefix = item[0][:6] # assuming that these always have the same length!
print output + prefix + "_ALL_DELTAS_TEST.shp"
arcpy.ET_GPErase(current, erase, output + prefix + "_ALL_DELTAS_TEST.shp", 0.0)

print "Script completed"