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
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
Or who can help combine the above-mentioned writings that the current script to obtain?
<SPAN class="kn" style="color: #0000ff;">import</SPAN> <SPAN class="nn">arcpy</SPAN><BR /><SPAN class="kn" style="color: #0000ff;">import</SPAN> <SPAN class="nn">os</SPAN><BR /><SPAN class="n">workspace</SPAN> <SPAN class="o">=</SPAN> <SPAN class="s" style="color: #a31515;">"c:/data"</SPAN><BR /><SPAN class="n">feature_classes</SPAN> <SPAN class="o">=</SPAN> <SPAN class="p">[]</SPAN><BR /><SPAN class="k" style="color: #0000ff;">for</SPAN> <SPAN class="n">dirpath</SPAN><SPAN class="p">,</SPAN> <SPAN class="n">dirnames</SPAN><SPAN class="p">,</SPAN> <SPAN class="n">filenames</SPAN> <SPAN class="ow" style="color: #0000ff;">in</SPAN> <SPAN class="n">arcpy</SPAN><SPAN class="o">.</SPAN><SPAN class="n">da</SPAN><SPAN class="o">.</SPAN><SPAN class="n">Walk</SPAN><SPAN class="p">(</SPAN><SPAN class="n">workspace</SPAN><SPAN class="p">,</SPAN><BR /> <SPAN class="n">datatype</SPAN><SPAN class="o">=</SPAN><SPAN class="s" style="color: #a31515;">"FeatureClass"</SPAN><SPAN class="p">,</SPAN><BR /> <SPAN class="nb">type</SPAN><SPAN class="o">=</SPAN><SPAN class="s" style="color: #a31515;">"Polygon"</SPAN><SPAN class="p">):</SPAN><BR /> <SPAN class="k" style="color: #0000ff;">for</SPAN> <SPAN class="n">filename</SPAN> <SPAN class="ow" style="color: #0000ff;">in</SPAN> <SPAN class="n">filenames</SPAN><SPAN class="p">:</SPAN><BR /> <SPAN class="n">feature_classes</SPAN><SPAN class="o">.</SPAN><SPAN class="n">append</SPAN><SPAN class="p">(</SPAN><SPAN class="n">os</SPAN><SPAN class="o">.</SPAN><SPAN class="n">path</SPAN><SPAN class="o">.</SPAN><SPAN class="n">join</SPAN><SPAN class="p">(</SPAN><SPAN class="n">dirpath</SPAN><SPAN class="p">,</SPAN> <SPAN class="n">filename</SPAN><SPAN class="p">))</SPAN>
Sorry don't use glob, this is better:
ArcGIS Help 10.1 (arcpy.da.walk)
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)
gp.AddMessage("--------------------------------------------------")
Have a look there:
glob – Filename pattern matching - Python Module of the Week
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.