Select to view content in your preferred language

Delete all empty feature class from folders and subfolder

1400
5
10-01-2014 06:04 AM
RuBi
by
New Contributor

Hi

maybe you can help to create a Python script, with which I could delete empty feature class and shp file from subdirectory and directory

Thanks!

Rubi

0 Kudos
5 Replies
JohannesBierer
Regular Contributor
0 Kudos
RuBi
by
New Contributor

Updated: I have a sample with which you can delete it from the directory or dataset, but it does not work subdirectory

# Import Python Modules

import arcgisscripting

#Create the geoprocessor object

gp = arcgisscripting.create(9.3)

# Set Workspace to list feature classes

#gp.workspace = "C:/temp/kme1.mdb/fdkme"

#Get Input Workspace as Argument

gp.workspace = gp.GetParameterAsText(0)

# Get List of Feature Classes in Workspace

lstFCs = gp.ListFeatureClasses()

print "\n"

gp.AddMessage("\n")

print "--------------------------------------------------"

#gp.AddMessage("--------------------------------------------------")

for fc in lstFCs:

  print "Processing " + fc

  gp.AddMessage("Processing " + fc)

  recCnt = int(gp.GetCount_management(fc).GetOutput(0))

  if recCnt == 0:

    print "Empty - Deleting " + fc

    gp.AddMessage("Empty - Deleting " + fc)

    gp.Delete_management(fc)

  else:

    print str(recCnt) + " Records - Keeping " + fc

    gp.AddMessage(str(recCnt) + " Records - Keeping " + fc)

print "--------------------------------------------------"

gp.AddMessage("--------------------------------------------------")

print "\n"

gp.AddMessage("\n")

0 Kudos
JohannesBierer
Regular Contributor

Sorry don't use glob, this is better:

ArcGIS Help 10.1 (arcpy.da.walk)

0 Kudos
RuBi
by
New Contributor

    Or who can help combine the above-mentioned writings that the current script to obtain?

Thanks!

import arcpy
import os
workspace = "c:/data"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
   datatype="FeatureClass",
   type="Polygon"😞
   for filename in filenames:
   feature_classes.append(os.path.join(dirpath, filename))

0 Kudos
Zeke
by
Regular Contributor III

Not sure what you're looking to do, but you need to indent the final line in your code above. This does nothing to delete empty shapefiles, though. Also would advise against using arcgisscripting; that's an older method. Not even sure if it would work to combine it with current arcpy.

You might also look at 10.1. os.path — Common pathname manipulations — Python 2.7.8 documentation and 15.1. os — Miscellaneous operating system interfaces — Python 2.7.8 documentation or Python os.listdir() Method

0 Kudos