From what I can gather is that arcpy.ListRasters(".TIF") is not storing the required rasters in "rasters". Since raster is empty, the program cannot iterate over rasters in "rasters". The error is in line 16 which is for raster in rasters:.
closed as unclear what you're asking by Aaron♦ Dec 11 '17 at 2:59
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
arcpy.env.workspace path -
F:\raster_test - probably doesn't exist or arcpy has no access to it. The
arcpy.ListRasters() will return an empty list
 if no rasters are found, but will return
None if the path isn't found:
Path does exist:
arcpy.env.workspace = r"C:\Temp" # Does Exist rasters = arcpy.ListRasters("*.tif) print rasters
Path does not exist:
arcpy.env.workspace = r"C:\Temp2" # Does NOT Exist rasters = arcpy.ListRasters("*.tif) print rasters
...also, backing up closer toward the beginning of the code is this faulty line (\r is an escape sequence in Python):
arcpy.env.workspace = "F:\raster_test" # Does NOT Exist
Provided F:\raster_test is a valid path (and contains rasters), then this should work fine:
arcpy.env.workspace = r"F:\raster_test" # Does Exist
See Python doc section on string literals.
I'm not sure what is causing your
arcpy.ListRasters() would return an empty list and never a
NoneType if there were no rasters found. Give this a shot:
import arcpy import os from arcpy.sa import ExtractByMask out_dir = r'F:\clipped_images' arcpy.env.workspace = r'F:\raster\test' arcpy.CheckOutExtension('Spatial') mask = r'F:\shapefile\Export_Output.shp' for raster in arcpy.ListRasters('*.tif'): output_raster = os.path.join(out_dir, os.path.splitext(raster) + '_clip.tif') ExtractByMask(raster, mask, output_raster) arcpy.CheckInExtension('Spatial')
I believe the various
List functions (
ListFeatureclasses, etc) always return
None if there are no matched types in the given workspace. Do a quick
if logic test for your list variable.
rasters = arcpy.ListRasters ("*.TIF") if rasters: your code here