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
Have a look there:
glob – Filename pattern matching - Python Module of the Week
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")
Sorry don't use glob, this is better:
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))
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