-3

enter image description here

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.

  • You have a * after tif, remove that. – Hornbydd Jan 31 '17 at 14:14
  • Make sure the workspace (F:\raster_test) actually exists. If you run this code on a directory that doesn't exist you'll get the NoneType error. – Dan Jurgella Jan 31 '17 at 15:47
  • 4
    Please edit your question to include your code snippet and your error messages as text not as images. This makes your code and errors searchable, and also makes it easier for potential answerers to copy/paste your code for their own testing – Midavalo Jan 31 '17 at 17:42
  • @Hornbydd I believe the extra * will allow .TIFF files to be included as well as .TIF – Midavalo Jan 31 '17 at 18:13
  • @Hornbydd Removing the extra * didn't make any difference. – Sujai Banerji Feb 3 '17 at 11:39
5

Your 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

Will output:

[]

Path does not exist:

arcpy.env.workspace = r"C:\Temp2"  # Does NOT Exist
rasters = arcpy.ListRasters("*.tif)
print rasters

Will output:

None
2

...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.

  • 1
    +1 @Sujai This is a classic python gotcha (on Windows at least). "\r" is the escape sequence for a carriage return. A more up to date (though the actual escape seqs haven't changed) is docs.python.org/2/reference/lexical_analysis.html#strings – user2856 Feb 7 '17 at 3:03
  • You're right, Luke....link in answer updated to the newer link. Lots of other good stuff on that page. – T. Wayne Whitley Feb 7 '17 at 3:17
1

I'm not sure what is causing your NoneType error, 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)[0] + '_clip.tif')
    ExtractByMask(raster, mask, output_raster)

arcpy.CheckInExtension('Spatial')
1

I believe the various List functions (ListTables, 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

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