I'm trying to run a loop through a number of rasters while using SetNull but keep receiving an error message. I have worked on these rasters individually using SetNull and it it works, but would need to do this multiple times so a loop would be so much better.
I am new to Python.
Update - sorted it out! The script below shows the changes in comments
# basic mask example: SetNull according to criteria and output set to 1
# Description: Returns NoData to any input cell with a value greater than 120 in the output raster, and the remaining output cells will be set to 1.
# Requirements: Spatial Analyst extension
# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *
# Set environment settings
env.workspace = r'C:\Users\Documents\ArcGIS\pythontest\acc'
arcpy.env.scratchWorkspace = r'C:\Users\Documents\ArcGIS\pythontest\acc'
#Set local variables
rasters = arcpy.ListRasters()
#inRaster = rasters
inFalseRaster = 1
whereClause = "value > 120"
#output folder is now different bc don't want to overwrite
outfolder = r'C:\Users\Documents\ArcGIS\pythontest\test'
#Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Loop through list of rasters
for raster in rasters:
#try/except has been dropped
# Execute SetNull outSetNull = SetNull(inRaster, inFalseRaster, whereClause)
#outSetNull = arcpy.gp.SetNull_sa(inRaster, inFalseRaster, "value > 120")
# updated with correct variable from the for loop
# rephrased the SetNull function. arcpy.gp.SetNull_sa is not correct
outSetNull = SetNull(raster, inFalseRaster, "value > 120")
#print the output before attempting to save
print(outSetNull)
#import os module
import os
#Set output name for each output to be the same as the input
# updated with correct variable from the for loop, attempting os module
outSetNull = os.path.join(outfolder, raster)
outSetNull.save(outras)
Old error message - refers to the line where outSetNull = arcpy.gp.SetNull_sa(raster, inFalseRaster, "value > 120") :
Runtime error Traceback (most recent call last): File "", line 31, in File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\geoprocessing_base.py", line 510, in return lambda *args: val(*gp_fixargs(args, True)) ExecuteError: ERROR 000877: Output raster: The grid or stack base name in C:\Users\Documents\ArcGIS\pythontest\acc\value > 120 has space.
arcpy.GetMessages()
?