I have created a simple script like that, which is working fine
import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("spatial")
arcpy.env.workspace = r'C:/2017/wgs84/project'
direction = arcpy.ListRasters("*WIND*","TIF")
print direction
speed = arcpy.ListRasters("*SPEED*","TIF")
print speed
for k in range(len(direction)):
outCon = Con(Raster(direction[k]) == 1, Raster(speed[k]), 0)
outCon.save('speed0' + str(k) + '.TIF')
print ('done')
print ('done final')
I have different wind direction files(rasters) in different folders and speed files in another one. This code works for what I need, only if I have pasted the speed files and the wind files in the same folder. What if I wanted to create a list from one folder and a list from another so as not having to copy paste every time?
I tried to define also a different workspace for direction and different for speed but apparently its not working(maybe the last env.workspace is "hold" in the memory?)
arcpy.env.workspace = r'C:/speed'
speed = arcpy.ListRasters("*SPEED*","TIF")
print speed
arcpy.env.workspace = r'C:/2017/wgs84/project'
direction = arcpy.ListRasters("*WIND*","TIF")
1 Answer 1
This will list the raster names without full path/workspace:
arcpy.env.workspace = r'C:/speed'
speed = arcpy.ListRasters("*SPEED*","TIF")
So when you change workspace afterwards, arcpy will not know where to find them.
Try adding full path to the raster lists using os.path.join and list comprehension:
import os
arcpy.env.workspace = r'C:/speed'
speed = arcpy.ListRasters("*SPEED*","TIF")
speed = [os.path.join(arcpy.env.workspace,r) for r in speed]
And then do the same for the wind rasters in the other folder.
Explore related questions
See similar questions with these tags.