3

I am trying to select all rasters using arcpy.ListRasters within all folders of a directory using arcpy.da.Walk. I'm using arcMap 10.2.

An example portion of the code I have:

workspace = ("...")
output = ("...")

walk = arcpy.da.Walk(workspace, topdown=True, datatype= "RasterDataset", type = "TIF")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        rasters = arcpy.ListRasters("*", "TIF")
        print rasters

Doesn't work. It results in :

None
None
None
None
None
None
None
None
None

This works and lists all raster names, but what I need is a list of the raster that I'm able to use for further processing:

workspace = ("...")
output = ("...")

walk = arcpy.da.Walk(workspace, topdown=True, datatype= "RasterDataset", type = "TIF")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        print filename

I'm trying to use ListRasters so that I can batch process those rasters. In most cases I could do something like:

import arcpy arcpy.env.workspace = (...)
rasters = arcpy.ListRasters("*", "TIF") 
for raster in rasters: arcpy.BatchBuildPyramids_management(raster) 

But in this case I have a lot of folders with rasters in each folder. I need to grab all those rasters into a raster list I can then batch process

3
  • 3
    You need to set your workspace environment for ListRasters... but you've already got the rasters in that workspace, your variable filenames contains the exact same as what ListRasters on that workspace would return, so why are you listing rasters? Is it to get the bands within the raster? if so arcpy.env.workspace = os.path.join(dirpath,filename) before calling ListRasters. Jan 16, 2018 at 22:42
  • 2
    @Pdavis327 Please edit your question to include any additional information as part of the question body rather than as comments.
    – Midavalo
    Jan 16, 2018 at 23:17
  • 1
    The point I'm trying to make is you already have your rasters listed, exactly as ListRasters("*", "TIF") would give you in your variable filenames, why do you need to call ListRasters again? If you want a list of all your rasters in the tree to use in a process start with an empty list and then append each rasters' full path which is os.path.join(dirpath,filename). BTW BatchBuildPyramids_management accepts a list as input, you don't need to call it for each raster, just call arcpy.BatchBuildPyramids_management(rasters) and it will iterate internally. Jan 16, 2018 at 23:31

1 Answer 1

6

You can use os.path.join() to combine dirpath and filename into a full file path. As @MichaelStimson points out, using ListRasters() in Walk is redundant. The Walk documentation provides more details. For example:

import arcpy, os

inws = r'X:\temp'
rasters = []

walk = arcpy.da.Walk(inws, topdown=True, datatype= "RasterDataset", type = "TIF")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        rasters.append(os.path.join(dirpath, filename))

The output is a list of rasters including their directory path.

>>> rasters
[u'X:\\temp\\dir1\\file1.tif',
 u'X:\\temp\\dir1\\dir1a\\file1a.tif',
 u'X:\\temp\\dir2\\file2.tif',
 u'X:\\temp\\dir3\\file3.tif']
>>> 

As a side note, if you are using Python >=3.5, you can recursively walk directories using glob.iglob(), which is very easy to implement. For example, the following produces the same list of rasters as in the previous example:

import glob
rasters = [x for x in glob.iglob(r'X:\temp\**\*.tif', recursive=True)]
1
  • 2
    There is also os.walk() which can be called like for dirpath, dirnames, filenames in os.walk(TopFolder): then for file in filenames:.. the two benefits of arcpy.walk is (1) you only get arcpy recognized objects (rasters, tables and feature classes for example) not all the miscellaneous files in your folder tree (2) arcpy.da.walk will list the contents of file and personal geodatabases in the folder tree as well as file objects - which is something only arcpy.da.walk will do native. Jan 17, 2018 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.