I am attempting to loop through two folders, one of input files (watershed polygons) and erase features (area boundaries) using arcpy.Erase_analysis
Here is my current code:
in_cover = "G:/Files/Watersheds"
erase_cover = "G:/Files/Areas"
out_cover = "G:/Output/Erase_output/"
arcpy.env.workspace = in_cover
for fc in [os.path.join(in_cover, x) for x in arcpy.ListFeatureClasses()]:
print fc
arcpy.env.workspace = erase_cover
for fc2 in [os.path.join(erase_cover, y) for y in arcpy.ListFeatureClasses()]:
print fc2
arcpy.Erase_analysis(in_features = fc, erase_features = fc2,
out_feature_class = out_cover + "UNP_" + fc)
Running this, I get this error:
ExecuteError: Failed to execute. Parameters are not valid. ERROR 000725: Output Feature Class: Dataset G:\Output\Erase_output\XXX1.shp already exists.
It seems like the second loop is returning all files in the folder instead of one. For example, if i remove the erase function and just print, here's what happens:
arcpy.env.workspace = in_cover
for fc in [os.path.join(in_cover, x) for x in arcpy.ListFeatureClasses()]:
print fc
arcpy.env.workspace = erase_cover
for fc2 in [os.path.join(erase_cover, y) for y in arcpy.ListFeatureClasses()]:
print fc2
This returns:
G:/Files/Watersheds\WS1.shp
G:/Files/Areas\Area1.shp
G:/Files/Areas\Area2.shp
G:/Files/Areas\Area3.shp
G:/Files/Areas\Area4.shp
G:/Files/Areas\Area5.shp
G:/Files/Watersheds\WS2.shp
G:/Files/Areas\Area1.shp
G:/Files/Areas\Area2.shp
G:/Files/Areas\Area3.shp
G:/Files/Areas\Area4.shp
G:/Files/Areas\Area5.shp
G:/Files/Watersheds\WS3.shp
G:/Files/Areas\Area1.shp
G:/Files/Areas\Area2.shp
G:/Files/Areas\Area3.shp
G:/Files/Areas\Area4.shp
G:/Files/Areas\Area5.shp
...And so on.
How can I fix this?
-
I thought that was the behavior you wanted. Your description reads as if you want to iterate over the watersheds and erase all the areas from each watershed. What result do you want?Tom– Tom2019年11月21日 16:28:44 +00:00Commented Nov 21, 2019 at 16:28
1 Answer 1
ListFeatureClasses doesn't return the full path to the feature class. It only returns the name. Then, within the outer loop, you change workspaces. So, in your call of Erase, you're passing G:/Files/Areas/[fc]
instead of then intended G:/Files/Watersheds/[fc]
.
To resolve this, where you iterate over the feature classes in "in_cover", use a list comprehension to create the full path:
for fc in [os.path.join(in_cover, x) for x in arcpy.ListFeatureClasses()]:
-
This helps with the issue of incorrectly iterating over the input names instead of paths. Thank you. However, now the second for loop is not working because it is spitting back the first iteration of the loop everytime. For example, if the first loop goes to watershed1, then watershed2..etc - the second for loop only returns area1 every time or essentially the entire list of shapefile areas. Any idea how to fix this?Fully Aquatic– Fully Aquatic2019年11月20日 19:17:05 +00:00Commented Nov 20, 2019 at 19:17
-
@Ecostrider, can you update your question to show your updated code? Thanks.Tom– Tom2019年11月20日 21:41:58 +00:00Commented Nov 20, 2019 at 21:41
-
I updated the questionFully Aquatic– Fully Aquatic2019年11月21日 14:04:43 +00:00Commented Nov 21, 2019 at 14:04
Explore related questions
See similar questions with these tags.