Loop raster dataset in Python

Discussion created by jhea2850 on Apr 4, 2013
Latest reply on Apr 4, 2013 by rafaelr
I am very new to Python and I want to extract raster by mask for a number of images. I am attempting to write python code for this and incorporate a loop so it will process every raster image within the folder without me manually changing it. This is my code below but it does not work. From the #Loop through list of rasters sections i.e. rasters.reset() all the errors begin to occur. Can someone please help me fix this coding.

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

# Set environment settings
env.workspace = "J:\PhD\Vegetation\MODIS13Q1\MODIS_NDVI_z56"

# Set local variables
inRaster = "J:\MODIS"
inMaskData = "arcreproarea.shp"
outRaster = "J:\\MODIS"

# Check out the ArcGIS Spatial Analyst extension license

# Get a list of the rasters in the workspace
    rasters = arcpy.ListRasters()

# Loop through the list of rasters
    raster = rasters.next()

while raster:
         # Set the outputname for each output to be the same as the input
        output = out_workspace + raster

        # Process: Extract by Mask
        arcpy.gp.ExtractByMask_sa(inRaster, inMaskData, outRaster)

        # Loop
        raster = rasters.next()