1

I am trying to merge shapefiles (I have multiple of them in one folder) but it keeps returning this error. I'm not sure what that means. I didn't change the path or the data.

import arcpy, os 
arcpy.env.overwriteOutput=1 
arcpy.env.workspace="C:/Users/data" 

fc = arcpy.ListFeatureClasses()
for shapefile in fc:
    if arcpy.ListFeatureClasses("","Polygon"):
       arcpy.Merge_management(fc,"merge_poly.shp")
    elif arcpy.ListFeatureClasses("","Point"):
       arcpy.Merge_management(fc,"merge_point.shp")
    elif arcpy.ListFeatureClasses("","Line"):
       arcpy.Merge_management(fc,"merge_line.shp")
    else:
        print("cannot merge")

ERROR 002948: The shape type of C:/Users/data\obce.shp is not the same as that of previously entered datasets. Failed to execute (Merge).

1
  • 1
    You cannot merge layers from differing topology classes. Your if tests are faulty.
    – Vince
    Mar 31 at 15:55
1

when you look at the list, you are not selecting the feature type but checking that the feature type is present in the entire list. I recommand to apply a filter based on the feature type to create your list, and you can test that the list is not empty to avoid errors.

if len(arcpy.ListFeatureClasses("*","Polygon"))>0:
    arcpy.Merge_management(arcpy.ListFeatureClasses("*","Polygon"),"merge_poly.shp")
if len(arcpy.ListFeatureClasses("*","Point"))>0:
    arcpy.Merge_management(arcpy.ListFeatureClasses("*","Point"),"merge_point.shp")

rem 1: using elif, you would only merge the polygons even if you have several lines or several points, therefore I suggest to use only "if" rem 2: merging if you have only one feture of one type is useless, except that it copies the file with your standard naming. You could use "> 1" instead of "> 0"

1
  • Thank you so much! I didn't know how to make it all work together. This really helped, it works, thank you.
    – Max
    Mar 31 at 16:06

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.