Using Walk() to navigate directories

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.

Getting ready

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.

How to do it…

Follow these steps to learn how to use the da.Walk() function to navigate directories and workspaces to reveal the structure of a geodatabase:

  1. In IDLE, create a new Python script called DAWalk.py and save it to the C:ArcpyBookdata folder.
  2. Import the arcpy, arcpy.da, and os modules:
    import arcpy.da as da
    import os
  3. First, we'll use 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)
  4. Save the file and run it to see output similar to what you see here:
    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
    
  5. Although 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.
  6. Comment out the code you just added.
  7. Add the following code block:
    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)
  8. The entire script should appear as follows:
    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))
  9. You can check your work by examining the C:ArcpyBookcodeCh8Walk.py solution file.
  10. Save and execute the script to see the following output. Notice how much cleaner the output is and that the actual feature class names contained within the geodatabase are printed out instead of the physical filenames:
    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
    

How it works…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset