I am trying to figure out a way to input corresponding raster datasets from two separate folders into an equation using arcpy.

I think it requires some sort of nested loop but I can't figure out how to do this so that only corresponding rasters are inputted into the equation.

For the sake of this example lets say I have two folders with rasters listed in them as follows:

Folder 1        Folder 2
WC_top_2008     WC_int_2008
WC_top_2009     WC_int_2009
WC_top_2010     WC_int_2010
WC_top_2011     WC_int_2011
WC_top_2012     WC_int_2012

If I wanted to multiply the corresponding years in each dataset by one another (i.e. WC_top_2008 * WC_int_2008, WC_top_2009 * WC_int_2009 etc.).

How would I do this in arcpy?

I am using ArcGIS 10 with spatial analyst.

  • will the file structure always look like this? will there always be a corresponding raster dataset? will numbers being equally matched? if so why not just square grab the string and square it.
    – Moggy
    Commented May 19, 2014 at 18:08
  • There will always be matching years in the names of the raster datasets. I'm not sure what you mean by "square grab the string and square it"? The equations will be more complex than the simple multiplication I have given as well, this was just a simplified example. Thank you. Commented May 20, 2014 at 8:06
  • use os.walk to make a list of the file names and then use a numpy array to multiply them. let me know if you need code
    – Moggy
    Commented May 20, 2014 at 20:52
  • Code would be useful thank you Moggy if you have the time spare? Still struggling to get my head around it. Thank you. Commented May 21, 2014 at 17:58

2 Answers 2


I would use zip to group your rasters by year, iterate over those, and then do the raster algebra. This assumes you have a corresponding years for all of the datasets and they are in the correct order.

import arcpy, os

arcpy.env.workspace = r'C:\path\to\your\ws'
a = arcpy.ListRasters(WC_top*)
b = arcpy.ListRasters(WC_int*)
outws = r'C:\temp'

c = zip(sorted(a),sorted(b))

for d in c:
    name = os.path.join(outws, 'outRaster' + d[-4:] + ".img") # e.g. C:\temp\outRaster2008.img
    output = Raster(d[0]) * Raster(d[1])
  • I tried this but when I got the error message "Runtime error <type 'exceptions.TypeError'>: expected a raster or layer name". When I did print c[0] it gave [u'WC_Top_2008', 'WC_int_2008']. So it seems that it calls on the group rather than the individual raster in the group? Commented May 22, 2014 at 18:36
  • Ah I see the issue, it should be output = Raster(d[0]) etc... I've edited your answer and given it the tick as I prefer this method to the one I came up with. Commented May 24, 2014 at 14:29
  • @Catchment_Jack Regarding the edit, as long as you have corresponding years in the folder, they do not have to be in the correct order, that is why I added the sorted() function to c = zip(sorted(a),sorted(b)).
    – Aaron
    Commented May 24, 2014 at 16:05

I think I've figured out a way if the rasters are placed into the same input folder by using calling the rasters according to a range.

To get the desired results from the above example the following code works:

env.workspace = # folder with both sets of input files
WC_top_list = arcpy.ListRasters(WC_top*)
WC_int_list = arcpy.ListRasters(WC_int*)

for k in range (5):
  output = Raster(WC_top_list[k]) * Raster(WC_int_list[k])
  output.save("output%s" % k) # saves each raster with the name output followed by the number in the range

The only issue with this is keeping the years in the output file name.

The answer is by no means perfect but it has got me to where I need to be.

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