Select to view content in your preferred language

Delete all empty shapefiles in a Folder

6165
11
Jump to solution
09-25-2014 02:06 AM
MittRamgobin
New Contributor

I want to delete all empty shapefiles in a folder. The following is not working:

 

 #Find empty shapefiles and delete them   

arcpy.env.workspace = r"S:\SupportServices\LGAShapefiles" 

shapefiles = arcpy.ListFeatureClasses() 

for shapefile in shapefiles: 

if arcpy.management.GetCount(shapefile)[0]=="0"

     arcpy.Delete_management(shapefile) 

     print shapefile + "Deleted" 

Tags (1)
0 Kudos
11 Replies
LilaDevkota
New Contributor

Try to delete shp files from Arc catalogue's catalogue tree.

0 Kudos
konstantBrr
New Contributor II
import arcpy, math, gc

# Workspace, overwrite 
arcpy.env.workspace = r"D:\testScript\My.gdb" 
arcpy.env.overwriteOutput = True

# INPUTS 
objects_input = r"D:\testScript\objects.shp" # must be polygons 
objects = "objects_lyr.shp" 
arcpy.MakeFeatureLayer_management(objects_input, objects)

# OUTPUTS, most temporal 
out_path=r"D:\testScript"
out_name="Hello.shp"
geometry_type = "POLYGON"
template = "study_quads.shp"
has_m = "DISABLED"
has_z = "DISABLED"
Myres = "Myres.shp"
arcpy.Delete_management(out_name) 
out_name_erase = "in_memory" + "\\" + "out_name_erase" 
polygon = "in_memory" + "\\" + "polygon" 
polygon_dissolve = "in_memory" + "\\" + "polygon_dissolve"
arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type)

# Parameters 
distance = 18 # distance for move in direction 
direction = 360 # direction in degrees (90 is from north to south) 
index = 0

# Set UpdateCursor
cur_objects = arcpy.da.UpdateCursor(objects, ("Dir"))
for row_objects in cur_objects:
    try:
        fid = row_objects[0]
        sql = '"Dir" = ' + str(index)
        index += 1

        # Initialize lists
        lines_list = []
        lines_created = []

        # Select current feature
        arcpy.SelectLayerByAttribute_management(objects, "NEW_SELECTION", sql)
        vertexes = "in_memory" + "\\" + "vertexes"

        # Convert object to vertexes
        arcpy.FeatureVerticesToPoints_management(objects, vertexes, "ALL")
        index_vertex = 0

        # Set SearchCursor for vertexes
        cur_vertexes = arcpy.da.SearchCursor(vertexes, ("SHAPE@XY"))
        for row_vertexes in cur_vertexes:
            vertex_coords_x = row_vertexes[0][0]
            vertex_coords_y = row_vertexes[0][1]

            # Define points coordinates
            point_move_x = vertex_coords_x - (distance) * math.cos(math.radians(direction))
            point_move_y = vertex_coords_y - (distance) * math.cos(math.radians(90 - direction))

            # Make list of points
            new_line = ([[vertex_coords_x, vertex_coords_y], [point_move_x, point_move_y]])
            lines_list.append(new_line)

            # From second cycle
            if index_vertex > 0:
                lines_vertexes = ([[vertex_coords_x, vertex_coords_y], start_line])
                lines_ends = ([[point_move_x, point_move_y], end_line])
                lines_list.append(lines_vertexes)
                lines_list.append(lines_ends)
            start_line = [vertex_coords_x, vertex_coords_y]
            end_line = [point_move_x, point_move_y]
            index_vertex = index_vertex + 1

        # Cycle that makes polylines from points
        for lines_step in lines_list:
            lines_created.append(arcpy.Polyline(arcpy.Array([arcpy.Point(*sour) for sour in lines_step])))

        arcpy.FeatureToPolygon_management(lines_created, polygon)
        arcpy.AggregatePolygons_cartography(polygon, polygon_dissolve, 1)

        # Final editing
        arcpy.Erase_analysis(polygon_dissolve, objects, out_name_erase)
        arcpy.Append_management(out_name_erase, out_name, "NO_TEST")
        arcpy.Delete_management("in_memory")
        arcpy.Delete_management(vertexes)
        start_line = []

        # Clear selection, memory and deleting temps
        arcpy.SelectLayerByAttribute_management(objects, "CLEAR_SELECTION")
        print ("Object number: " + str(index - 1) + " -- done.")
        gc.collect()


    # Catch errors
    except Exception as e:
        pass
        print ("Error:")
        print (e)
        print ("\n")
        index += 1
0 Kudos