In this recipe, you will learn how to generate data names in a catalog tree using the Arcpy Walk()
function. Though similar to the Python os.walk()
function, the da.Walk()
function provides some important enhancements related to geodatabases.
The Walk()
function, which is part of arcpy.da
, generates data names in a catalog tree by walking the tree top-down or bottom-up. Each directory or workspace yields a tuple containing the directory path, directory names, and filenames. This function is similar to the Python os.walk()
function but it has the added advantage of being able to recognize geodatabase structures. The os.walk()
function is file-based so it isn't able to tell you information about geodatabase structures while arcpy.da.walk()
can do so.
Follow these steps to learn how to use the da.Walk()
function to navigate directories and workspaces to reveal the structure of a geodatabase:
DAWalk.py
and save it to the C:ArcpyBookdata
folder.arcpy
, arcpy.da
, and os
modules:import arcpy.da as da import os
os.walk()
to obtain a list of filenames in the current directory. Add this code:print("os walk") for dirpath, dirnames, filenames in os.walk(os.getcwd()): for filename in filenames: print(filename)
a00000001.gdbindexes a00000001.gdbtable a00000001.gdbtablx a00000002.gdbtable a00000002.gdbtablx a00000003.gdbindexes a00000003.gdbtable a00000003.gdbtablx a00000004.CatItemsByPhysicalName.atx a00000004.CatItemsByType.atx a00000004.FDO_UUID.atx a00000004.freelist a00000004.gdbindexes a00000004.gdbtable a00000004.gdbtablx
os.walk()
can be used to print all filenames within a directory, you'll notice that it doesn't have an understanding of the structure of Esri GIS format datasets, such as file geodatabases. Files, such as a000000001.gdbindexes
, are physical files that make up a feature class but os.walk()
can't tell you the logical structure of a feature class. In the next step, we'll use da.walk()
to resolve this problem.print("arcpy da walk") for dirpath, dirnames, filenames in da.Walk(os.getcwd(),datatype="FeatureClass"): for filename in filenames: print(os.path.join(dirpath, filename)
import arcpy.da as da import os print("os walk") for dirpath, dirnames, filenames in os.walk(os.getcwd()): for filename in filenames: print(filename) print("arcpy da walk") for dirpath, dirnames, filenames in da.Walk(os.getcwd(),datatype="FeatureClass"): for filename in filenames: print(os.path.join(dirpath, filename))
C:ArcpyBookcodeCh8Walk.py
solution file.C:ArcpyBookdataBuilding_Permits.shp C:ArcpyBookdataBurglaries_2009.shp C:ArcpyBookdataStreams.shp C:ArcpyBookdataCityOfSanAntonio.gdbCrimes2009 C:ArcpyBookdataCityOfSanAntonio.gdbCityBoundaries C:ArcpyBookdataCityOfSanAntonio.gdbCrimesBySchoolDistrict C:ArcpyBookdataCityOfSanAntonio.gdbSchoolDistricts C:ArcpyBookdataCityOfSanAntonio.gdbBexarCountyBoundaries C:ArcpyBookdataCityOfSanAntonio.gdbTexas_Counties_LowRes C:ArcpyBookdataCityOfSanAntonio.gdbBurglary C:ArcpyBookdataTravisCountyBuildingPermits.shp C:ArcpyBookdataTravisCountyCensusTracts.shp C:ArcpyBookdataTravisCountyCityLimits.shp C:ArcpyBookdataTravisCountyFloodplains.shp C:ArcpyBookdataTravisCountyHospitals.shp C:ArcpyBookdataTravisCountySchools.shp C:ArcpyBookdataTravisCountyStreams.shp C:ArcpyBookdataTravisCountyStreets.shp C:ArcpyBookdataTravisCountyTravisCounty.shp C:ArcpyBookdataWildfiresWildlandFires.mdbFireIncidents
The da.Walk()
function accepts two parameters including the top-level workspace that will be retrieved (the current working directory), as well as the data type that will be used to filter the returned list. In this case, we retrieved only feature class-related files. The Walk()
function returns a tuple containing the directory path, directory names, and filenames.