Delete all empty shapefiles in a Folder

5870
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
1 Solution

Accepted Solutions
RiyasDeen
Occasional Contributor III

Hi Mitt,

It's the period character in your shapefile name that's creating the issue.

Change line 43 in above code to replace . with _

basefileName = arcpy.Describe(Struc).baseName.replace('.','_') + LGA

View solution in original post

11 Replies
RiyasDeen
Occasional Contributor III

Hi Mitt,

It should be

if int(arcpy.GetCount_management(shapefile).getOutput(0)) == 0:

have a look at example 2 ArcGIS Desktop

0 Kudos
MittRamgobin
New Contributor

Hi Riyas,

Thank you for your input.

I am now getting the following error:

ExecuteError: Failed to execute. Parameters are not valid.

ERROR 000732: Input Rows: Dataset GISTRAN.SubstationCOOROW, SHIRE OF.shp does not exist or is not supported

Failed to execute (GetCount).

What is going on?

0 Kudos
RiyasDeen
Occasional Contributor III

Hi Mitt,

Sorry, Get Count accepts a layer input. We need to create a layer from the shapefile and pass it as input to the get count tool. Use below code, tested. Mind the line intents

arcpy.env.workspace = r"C:\tempdelete\Test_road"

shapefiles = arcpy.ListFeatureClasses()

for shapefile in shapefiles:

  layerName = arcpy.Describe(shapefile).baseName

  arcpy.AddMessage(layerName)

  arcpy.MakeFeatureLayer_management (shapefile, layerName)

  arcpy.AddMessage(arcpy.GetCount_management(layerName).getOutput(0))

  if int(arcpy.GetCount_management(layerName).getOutput(0)) == 0:

  arcpy.Delete_management(shapefile)

0 Kudos
MittRamgobin
New Contributor

Hi Riyas,

This time it gets stuck at the MakeFeatureLayer

ERROR 000732: Input Features: Dataset GISTRAN.SubstationCOOROW, SHIRE OF.shp does not exist or is not supported

Failed to execute (MakeFeatureLayer).

0 Kudos
MittRamgobin
New Contributor

import arcpy
import sys
import os


# Script to create LGA Map with WP Infrastructure


# Define variables


arcpy.env.workspace = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde"
arcpy.env.overwriteOutput = True
#LGA = arcpy.GetParameterAsText(0) # Input Required Local Government Area
LGA ="COOROW, SHIRE OF"


#Define Layers to Intersect


#1. Transmission Layers
T330OH = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_330kV_OH_Carrier"    
T330UG = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_330kV_UG_Cable"
T220OV = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_220kV_OH_Carrier"
T132OH = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_132kV_OH_Carrier"
T132UG = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_132kV_UG_Cable"
T66OV = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_66kV_OH_Carrier"
T66UG = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_66kV_UG_Cable"
T33OH = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_33kV_OH_Carrier"
T33UG = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_33kV_UG_Cable"
TransStruc = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_Structure"
Substation = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Substation"
#Group Transmission Values:
Transmission = (T330OH, T330UG, T220OV, T132OH, T132UG, T66OV, T66UG, T33OH, T33UG, TransStruc, Substation)
 
#2. Distrubution Layers
POLES = os.path.join("ARCFM.Structure", "ARCFM.Pole")
HVOH = os.path.join("ARCFM.Distribution_HV", "ARCFM.Conductor_HV")
HVUG = os.path.join("ARCFM.Distribution_HV", "ARCFM.Cable_HV")
LVOH = os.path.join("ARCFM.Distribution_LV", "ARCFM.Conductor_LV")
LVUG = os.path.join("ARCFM.Distribution_LV", "ARCFM.Cable_LV")
#Group Distribution Values
Distribution = (POLES, HVOH, HVUG, LVOH, LVUG)
                                  
#Select Required LGA
arcpy.MakeFeatureLayer_management("GISEXT.Local_Government_Authority", "LGAlyr")
fieldName = "NAME"
Query = "\""+fieldName+"\"='"+LGA+"'"
arcpy.SelectLayerByAttribute_management ("LGAlyr", "NEW_SELECTION", Query)
###arcpy.SelectLayerByAttribute_management ("LGAlyr", "NEW_SELECTION", """ "NAME" = 'ALBANY, CITY OF' """) - working code


for Struc in Transmission:
    outputPath = r"S:\SupportServices\LGAShapefiles"
    basefileName = arcpy.Describe(Struc).baseName + LGA
    fileFormat= '.shp'
    finalDest = os.path.join(outputPath,basefileName+fileFormat)
    arcpy.Clip_analysis(Struc, "LGAlyr",finalDest)
#arcpy.Clip_analysis(Struc,"LGAlyr", finalDest)
print "Transmission Shapefile Created"


##for Struc in Distribution:
##    outputPath = r"S:\SupportServices\LGAShapefiles"
##    basefileName = arcpy.Describe(Struc).baseName + LGA
##    fileFormat= '.shp'
##    finalDest = os.path.join(outputPath,basefileName+fileFormat)
##    arcpy.Clip_analysis(Struc, "LGAlyr",finalDest)





#Find empty shapefiles and delete them
arcpy.env.workspace = r"S:\SupportServices\LGAShapefiles"


shapefiles = arcpy.ListFeatureClasses()


for shapefile in shapefiles:
    layerName = arcpy.Describe(shapefile).baseName
    arcpy.AddMessage(layerName)
    arcpy.MakeFeatureLayer_management (shapefile, layerName)
    arcpy.AddMessage(arcpy.GetCount_management(layerName).getOutput(0))
    if int(arcpy.GetCount_management(layerName).getOutput(0)) == 0:
        arcpy.Delete_management(shapefile)




0 Kudos
RiyasDeen
Occasional Contributor III

Hi Mitt,

It's the period character in your shapefile name that's creating the issue.

Change line 43 in above code to replace . with _

basefileName = arcpy.Describe(Struc).baseName.replace('.','_') + LGA

MittRamgobin
New Contributor

Hi Riyas,

Now there is this

ExecuteError: ERROR 000210: Cannot create output S:\SupportServices\LGAShapefiles\GISTRAN_Trans_330kV_OH_CarrierKONDININ, SHIRE OF.shp

ERROR 000354: The name contains invalid characters

Failed to execute (Clip).

0 Kudos
RiyasDeen
Occasional Contributor III

, in your shapefile name is creating issue with clip. Interestingly if your shape file name is GISTRAN.Trans_330kV_OH_CarrierKONDININ, SHIRE OF.shp (with period) it's slipping through

. is creating issue with make feature layer

It's better to avoid special characters in your shape file name all together.

Change your LGA name to Shire of KONDININ without , separator.

0 Kudos
MittRamgobin
New Contributor

Works like a treat now!

Thank you Riyas

0 Kudos