Walk

Zusammenfassung

Gibt Datennamen in Verzeichnis- und Datenbankstrukturen zurück, indem das Verzeichnis von oben nach unten oder von unten nach oben durchlaufen wird. Jedes Verzeichnis bzw. jeder Workspace ergibt einen Dreier-Tupel: Verzeichnispfad, Verzeichnisnamen und Dateinamen.

Diskussion

Das os-Modul enthält eine os.walk-Funktion, die verwendet werden kann, um eine Verzeichnisstruktur zu durchlaufen und Daten zu suchen. Die os.walk-Funktion ist dateibasiert. Datenbankinhalte wie Geodatabase-Feature-Classes, Tabellen oder Raster werden nicht erkannt. Zur Verbesserung der Performance wird empfohlen, os.walk für dateibasierte Formate zu verwenden. Die arcpy.da.Walk-Funktion kann zum Katalogisieren von Daten verwendet werden.

Syntax

Walk (top, {topdown}, {onerror}, {followlinks}, {datatype}, {type})
ParameterErläuterungDatentyp
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. Modifyingdirnames when topdown is False is ineffective, because in bottom-up mode, the workspaces indirnames are generated before dirpath is generated.

(Der Standardwert ist 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.

Hinweis:

The file name is available as the filename attribute of the exception object.

(Der Standardwert ist None)

Function
followlinks

By default, Walk does not visit connection files. Set followlinks to True to visit connection files.

(Der Standardwert ist False)

Boolean
datatype

Specifies the data type that will be used to limit the results.

  • AnyAll data types are returned. This is equivalent to using None or skipping the argument.
  • CadDrawingCAD files (.dwg, .dxf, and .dgn) are returned.
  • FeatureClassFeature classes in a geodatabase and shapefiles (.shp) are returned.
  • FeatureDatasetFeature datasets in a geodatabase are returned.
  • GeometricNetworkGeometric networks in a geodatabase are returned.
  • LasDatasetLAS dataset files (.las and .lasd) are returned.
  • LayerLayer files (.lyrx) are returned.
  • LocatorLocator files (.loc) are returned.
  • Map ArcGIS Pro projects (.aprx files), ArcGIS Pro map documents (.mapx files), and ArcMap documents (.mxd files) are returned.
  • MosaicDatasetMosaic datasets in a geodatabase are returned.
  • NetworkDatasetNetwork datasets in a geodatabase are returned.
  • RasterCatalogRaster catalogs in a geodatabase are returned.
  • RasterDatasetRaster datasets in a geodatabase or in a folder are returned.
  • RelationshipClassRelationship classes in a geodatabase are returned.
  • RepresentationClassRepresentation classes in a geodatabase are returned.
  • TableTables in a geodatabase or folder and sheets in Excel workbooks are returned.
  • TerrainTerrain datasets are returned.
  • TinTIN surfaces in a geodatabase are returned.
  • ToolTools in a toolbox are returned.
  • TopologyTopologies in a geodatabase are returned.

Multiple data types are supported if they're entered as a list or tuple.

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
    datatype=['MosaicDataset', 'RasterDataset']):

(Der Standardwert ist None)

String
type

Specifies whether feature and raster data types will be further limited by type.

  • AllAll types are returned. This is equivalent to using None or skipping the argument.
  • AnyAll types are returned. This is equivalent to using None or skipping the argument.

Valid feature types are the following:

  • MultipatchMultipatch feature classes are returned.
  • MultipointMultipoint feature classes are returned.
  • PointPoint feature classes are returned.
  • PolygonPolygon feature classes are returned.
  • PolylinePolyline feature classes are returned.

Valid raster types are the following:

  • BIL Esri Band Interleaved by Line file
  • BIP Esri Band Interleaved by Pixel file
  • BMP Bitmap graphic raster dataset format
  • BSQ Esri Band Sequential file
  • DAT ENVI DAT file
  • GIF Graphic Interchange Format for raster datasets
  • GRID Esri Grid raster dataset format
  • IMG ERDAS IMAGINE raster data format
  • JP2 JPEG 2000 raster dataset format
  • JPG Joint Photographic Experts Group raster dataset format
  • PNG Portable Network Graphic raster dataset format
  • TIF Tag Image File Format for raster datasets

Multiple data types are supported if they're entered as a list or tuple.

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
    datatype='FeatureClass', type=['Polygon', 'Polyline']):

(Der Standardwert ist None)

String
Rückgabewert
DatentypErläuterung
Generator

Ergibt einen Dreier-Tupel aus Workspace, Verzeichnisnamen und Dateinamen.

  • dirpath ist der Pfad zum Workspace als Zeichenfolge.
  • dirnames ist eine Liste mit Namen von Unterverzeichnissen und anderen Workspaces in dirpath.
  • filenames ist eine Liste mit Namen von Inhalten in dirpath, die keine Workspaces sind.
Hinweis:

Die Namen in den Listen umfassen nur den Basisnamen und keine Pfadkomponenten. Für einen vollständigen (mit "top" beginnenden) Pfad zu einer Datei oder einem Verzeichnis in dirpath verwenden Sie os.path.join(dirpath, name).

Codebeispiel

Walk – Beispiel 1

Verwenden Sie die Walk-Funktion, um Polygon-Feature-Classes zu katalogisieren.

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))
Walk – Beispiel 2

Verwenden Sie die Walk-Funktion, um Raster-Daten zu katalogisieren. Jedes Raster im Ordner namens back_up wird ignoriert.

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))

Verwandte Themen