I am writing a script to loop through multiple folders and calculate mean of the raster of each folder's data sets and save it in same folder. Below script i am working, but its not producing the mean rasters for each year folder, after running the script its generating only name of "mean" raster in main folder, and overlapping it .

Below images showing the name of the raster in one folder, another folder the raster name is same, only year value is changed in different year.
here how to take the year value in name of mean raster (E.g. for 2001 folder, output mean raster name would be Mean_RAIN_2001.tiff)

enter image description here


import arcpy, os
from arcpy import env
arcpy.env.overwriteOutput = True
env.workspace = r"D:\Test"
outraster = env.workspace

walk = arcpy.da.Walk(env.workspace, topdown=True, datatype="RasterDataset")
for dirpath, dirnames, filenames in walk:
    print dirpath
    rasterList = arcpy.ListRasters("TIF")
    print rasterList

    for Rasters in rasterList:
        rasMean = CellStatistics(Rasters,"MEAN", "DATA")
        file_name_only = os.path.splitext(Rasters)[0]
        tifname = file_name_only[-4:]
        print rasMean

Printing statement: its printing only folder name,

  • As you will see in the code snippet recommendations it is best to remove any try/except statements while testing.
    – PolyGeo
    Mar 25 '17 at 10:02
  • yes, i removed try/except statements and rerun the code, now its producing error
    – SWAT
    Mar 25 '17 at 10:11
  • 1
    It would be helpful if you could include the full error message, and the output from any print statements from running this code.
    – PolyGeo
    Mar 25 '17 at 10:19
  • i did little change the code, now its not showing any error but generating only one output name of "mean" in test folder. even output of raster not goes to subfolder to save it in each folder.
    – SWAT
    Mar 25 '17 at 10:24
  • 2
    Where's the output of those two print statements? I would print rasterList too.
    – PolyGeo
    Mar 25 '17 at 11:47

I can see several things wrong with this script. Firstly you keep overwriting your output with this line:


The reasons for that is outraster never changes as it comes from the environment settings which you set to d:\test. outraster needs to change with each level that you are looping over (a concatenation of dirpath and dirnames).

I think it would be a good idea if you experiment with the walk() function and understand what it is generating as this is the source of your problem.

Secondly the tool CellStatistics takes as input a list of rasters, but your current code is building the list and calling the tool within your "for file in filenames" loop which is horribly inefficient. You need to build the list then feed that into the tool so all you need to do is remove the indentation from the 3 lines after the append call so they fall outside the loop.

  • thank you. can i get some idea how to reconstruct the code .
    – SWAT
    Mar 29 '17 at 6:16
  • please help me to reconstruct the code
    – SWAT
    Mar 29 '17 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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