Resumen
Genere nombres de datos en una estructura de directorio/base de datos recorriendo el árbol de arriba abajo o de abajo arriba. Cada directorio/espacio de trabajo del árbol genera una tupla de tres: ruta de directorio, nombres de directorio y nombres de archivo.
Debate
El módulo os de Python incluye una función os.walk que se puede usar para atravesar un árbol de directorio y buscar datos. os.walk se basa en archivos y no reconoce el contenido de la base de datos, como clases de entidad, tablas o rásteres de geodatabase. arcpy.da.Walk se puede usar para catalogar datos.
Sintaxis
Walk (top, {topdown}, {onerror}, {followlinks}, {datatype}, {type})
Parámetro | Explicación | Tipo de datos |
top | The top-level workspace that will be used. | String |
topdown | If topdown is True or not specified, the tuple for a directory is generated before the tuple for any of its workspaces (workspaces are generated top-down). If topdown is False, the tuple for a workspace is generated after the tuple for all of its subworkspaces (workspaces are generated bottom-up). When topdown is True, the dirnames list can be modified in-place, and Walk will only recurse into the subworkspaces whose names remain in dirnames. This can be used to limit the search, impose a specific order of visiting, or even to inform Walk about directories the caller creates or renames before it resumes Walk again. Modifying dirnames when topdown is False is ineffective, because in bottom-up mode the workspaces in dirnames are generated before dirpath itself is generated. (El valor predeterminado es True) | Boolean |
onerror | Errors are ignored by default. The onerror function will be called with an OSError instance. The function can be used to report the error and continue with the walk or raise an exception to abort. (El valor predeterminado es None) | Function |
followlinks | By default, Walk does not walk into connection files. Set followlinks to True to visit connection files. (El valor predeterminado es False) | Boolean |
datatype | The data type to limit the results returned. Valid data types are the following:
Multiple data types are supported if entered as a list or tuple.
(El valor predeterminado es None) | String |
type | Feature and raster data types can be further limited by type.
Valid feature types are the following:
Valid raster types are:
Multiple data types are supported if entered as a list or tuple.
(El valor predeterminado es None) | String |
Valor de retorno
Tipo de datos | Explicación |
Generator | Genera una tupla de tres que incluye el espacio de trabajo, nombres de directorio y nombres de archivo.
|
Muestra de código
Ejemplo 1 de Walk
Use la función Walk para catalogar clases de entidad poligonal.çç
import arcpy
import os
workspace = "c:/data"
feature_classes = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")
for dirpath, dirnames, filenames in walk:
for filename in filenames:
feature_classes.append(os.path.join(dirpath, filename))
Ejemplo 2 de Walk
Use la función Walk para catalogar datos ráster. Se omitirán todos los rásteres de una carpeta llamada back_up.
import arcpy
import os
workspace = "c:/data"
rasters = []
walk = arcpy.da.Walk(workspace, topdown=True, datatype="RasterDataset")
for dirpath, dirnames, filenames in walk:
# Disregard any folder named 'back_up' in creating list of rasters
if "back_up" in dirnames:
dirnames.remove('back_up')
for filename in filenames:
rasters.append(os.path.join(dirpath, filename))