Walk
Résumé
The Walk function returns data names in directory and database structures by moving through the tree from the top down or the bottom up. Each directory or workspace yields a tuple of three: directory path, directory names, and file names.
Discussion
The os module includes the os.walk function that can be used to move through a directory tree and find data. The os.walk function is file based and does not recognize database content such as geodatabase feature classes, tables, or rasters. For better performance, it is recommended that you use os.walk for file-based formats. The arcpy.da.Walk function can be used to catalog data.
Syntaxe
Walk(workspace, {topdown}, {onerror}, {followlinks}, {datatype}, {type})
| Le paramètre | Explication | Type de données |
|---|---|---|
|
workspace |
The top-level workspace that will be used. |
String |
|
topdown |
If When The default value is True. |
Boolean |
|
onerror |
Errors are ignored by default. The This function can be used to report the error and continue with Remarque :The file name is available as the The default value is None. |
Function |
|
followlinks |
By default, The default value is False. |
Boolean |
|
datatype |
Specifies the data type that will be used to limit the results.
Multiple data types are supported if they're entered as a list or tuple.
The default value is None. |
String |
|
type |
Specifies whether feature and raster data types will be further limited by type.
Valid feature types are the following:
Valid raster types are the following:
Multiple data types are supported if they're entered as a list or tuple.
The default value is None. |
String |
Valeur de retour
| Type de données | Explication |
|---|---|
|
Generator |
Yields a tuple of three that includes the workspace, directory names, and file names.
Remarque :Names in the lists include only the base name; no path components are included. For a full path (which begins with top) to a file or directory in |
Exemple de code
Use the Walk function to catalog polygon feature classes in the workspace.
The code will include polygon feature classes in any feature datasets in a file geodatabase.
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))
Use the Walk function to catalog raster data. Any rasters in a folder named back_up will be ignored.
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))