サマリー
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.
説明
The os module includes an 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.
構文
Walk (top, {topdown}, {onerror}, {followlinks}, {datatype}, {type})
パラメーター | 説明 | データ タイプ |
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 trom the top down). If topdown is False, the tuple for a workspace is generated after the tuple for all of its subworkspaces (workspaces are generated from the 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 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 is generated. (デフォルト値は次のとおりです True) | Boolean |
onerror | Errors are ignored by default. The onerror function will be called with an OSError instance. This function can be used to report the error and continue with Walk or raise an exception to cancel. 注意:The file name is available as the filename attribute of the exception object. (デフォルト値は次のとおりです None) | Function |
followlinks | By default, Walk does not visit connection files. Set followlinks to True to visit connection files. (デフォルト値は次のとおりです 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.
(デフォルト値は次のとおりです 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.
(デフォルト値は次のとおりです None) | String |
データ タイプ | 説明 |
Generator | Yields a tuple of three that includes the workspace, directory names, and file names.
注意: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 dirpath, use os.path.join(dirpath, name). |
コードのサンプル
Use the Walk function to catalog polygon feature classes.
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))