2

I have a Python Toolbox and I want to use a directory to read different shapefiles. But when I run the script It only works for the first shape.

fd is the directory and the code is:

fd= parameters[0].valueAsText
raster= parameters[1].valueAsText

mar="Z:\\Datos_centralizados\\OSM_coastlines\\water_polygons.shp"
out= "Z:\\EMSV-057_AOIs_Reshape\\5_Process\\1_TaskA\\ModelBuilder\\MB_pruebas.gdb"
#-------------------------
    

#leemos los shapes de la carpeta y los metemos a una lista
    
flist = []
for root,folder,files in os.walk(fd):
        for file in files:
            if os.path.isfile(os.path.join(root,file)) and file.endswith('.shp'):
                flist.append(os.path.join(root,file))
            

for z in flist:
    shape=z

    # Cogemos datos alfanumericos del AOI
    desc = arcpy.Describe(shape)
    
    # aplicamos la herramienta Erase para quedarnos con la tierra
    erase=arcpy.Erase_analysis(shape,mar, out+"\\Tierra", cluster_tolerance="")
            
    
    # Estadisticos
    tZONA=arcpy.gp.ZonalStatisticsAsTable_sa(erase, "FID", raster, out+"\\tabla_Zona", "NODATA", "SUM") 
    
3
  • Is arcpy.env.overwriteOutput set to True?
    – Bjorn
    Commented Sep 16, 2020 at 17:05
  • I delete all the variables at the final of each iteration
    – MikV89
    Commented Sep 17, 2020 at 4:46
  • Are you using Python 2 or 3?
    – Aaron
    Commented Sep 17, 2020 at 20:09

1 Answer 1

5

Here are four approaches. The first uses the arcpy Walk function. The second, third, and fourth approaches use built-in python modules. The fourth approach is the one you provided.

Here are the time benchmarks to recursively search a large directory:

Method 1:  15.0 seconds (arcpy.Walk)
Method 2: 140.0 seconds (os.Walk and fnmatch.filter)
Method 3:   4.0 seconds (glob.glob)
Method 4: 287.0 seconds (os.Walk)

Clearly Method 3 is the fastest--although this is because the search is relatively simple (i.e. looking for an extension that meets certain conditions). Note that method 3 requires the use of glob running Python 3. Method 1 is preferable if you need to isolate, for example point feature classes or other specific datatypes.


directory = r'C:\path\to\your\directory'

# Method 1 (Python 2.7)
import arcpy
import os

feature_classes = []

walk = arcpy.da.Walk(directory, datatype = "FeatureClass")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        check = os.path.join(dirpath, filename)
        if os.path.isfile(check) and check.endswith(".shp"):
            feature_classes.append(os.path.join(dirpath, filename))

# Method 2 (Python 2.7)
import fnmatch
import os

matches = []
for root, dirnames, filenames in os.walk(directory):
    for filename in fnmatch.filter(filenames, '*.shp'):
        if os.path.isfile(os.path.join(root, filename)):
            matches.append(os.path.join(root, filename))

# Method 3 (Python 3.8)
import glob, os

shps = [x for x in glob.glob(os.path.join(directory, "**", "*.shp"), recursive = True) if os.path.isfile(x) and x.endswith(".shp")]

# Method 4 (Python 2.7)
import arcpy
import os

flist = []
for root,folder,files in os.walk(directory):
        for f in files:
            if os.path.isfile(os.path.join(root,f)) and f.endswith('.shp'):
                flist.append(os.path.join(root,f))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.