Skip to main content
2 of 2
spelling, syntax
Kirk Kuykendall
  • 25.8k
  • 8
  • 67
  • 155

For loop can not find the next raster object in the geodatabase. OSError: "" does not exist

I'm trying to convert a list of rasters in a geodatabase to integer type and rename them with a specific folder name as the prefix. This first block of code moves the ASCII rasters into the geodatabase. And it works fine.

import arcpy, os
from arcpy import env
from arcpy.sa import *

# Set the environment variables
sr = arcpy.SpatialReference(26911)
env.overwriteOutput=True
env.extent = 'MAXOF'


### Change to the desired scenario/replicate
# "Scenario1_Rep2"
# "Scenario1_Rep6"
# "Scenario2_Rep2"
# "Scenario2_Rep5"
# "Scenario3_Rep2"
# "Scenario3_Rep7"
# "Scenario4_Rep2"
# "Scenario4_Rep9"

# set workspace
scene = "Scenario1_Rep2"
path = "C:\\GIS\\PROJECT\\LakeTahoeWest\\Data\\LANDIS\\WaterQualityAnalysis\\"
env.workspace = os.path.join(path, scene)

# define output gdb
outgdb = "C:\\GIS\\PROJECT\\LakeTahoeWest\\Data\\LANDIS\\WaterQualityAnalysis\\WaterQualityAnalysis.gdb"

# Get a list of rasters from the workspace
rasters = arcpy.ListRasters("*", "ALL")

# loop through raster list 
for raster in rasters:
    # create describe object of each raster
    desc = arcpy.Describe(raster)
    inras = desc.CatalogPath
    print ("Copied " + desc.baseName + " to fgdb.")
    # create new raster in gdb
    arcpy.RasterToGeodatabase_conversion(inras, outgdb)

This next block of code is where the error happens:

import arcpy, os

# Name = "WaterQualityAnalysis"
# wFolder = r"C:\GIS\PROJECT\LakeTahoeWest\Data\LANDIS\WaterQualityAnalysis"
# GDB = os.path.join(str(Name) + ".gdb")

# env.workspace = os.path.join(str(wFolder), str(GDB))
# print (arcpy.Exists(env.workspace))

env.workspace ="C:\\GIS\\PROJECT\\LakeTahoeWest\\Data\\LANDIS\\WaterQualityAnalysis\\WaterQualityAnalysis.gdb"

# Get a list of rasters from the workspace
rasters = arcpy.ListRasters("*_img", "ALL")
print(rasters)
# loop through raster list 
for raster in rasters:

    # create describe object of each raster
    desc = arcpy.Describe(raster)
    inras = desc.CatalogPath
    print(inras)
    # project the raster to UTM Zone 11N
    arcpy.DefineProjection_management(inras, sr)
    # get the new name to print
    dsc = arcpy.Describe(inras)
    year = str(dsc.baseName[0:-4].split('_')[-1])
    event = str(dsc.baseName[0:-4].split('_')[0])

    print ("Created: " + dsc.baseName)
    
    # convert to integer type and save to gdb
    outras = Int(inras)
    outras.save(outgdb + "\\" + scene + "_" + event + "_" + year)
    # return success message
    desc = arcpy.Describe(outras)
    print ("Created: "+ desc.baseName)
    print ("Deleting " + dsc.baseName)
    arcpy.Delete_management(inras)

As you can see, I've messed around with the path names a bit thinking that was the issue.

Here is the output and error I'm getting:

['fire_intensity_41_img', 'fire_intensity_42_img', 'fire_intensity_43_img', 'fire_intensity_44_img', 'fire_intensity_45_img', 'fire_intensity_46_img', 'fire_intensity_47_img', 'fire_intensity_48_img', 'fire_intensity_49_img', 'fire_intensity_50_img']
C:\GIS\PROJECT\LakeTahoeWest\Data\LANDIS\WaterQualityAnalysis\WaterQualityAnalysis.gdb\fire_intensity_41_img
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-14-44c2fe5a12b6> in <module>
     17 
     18     # create describe object of each raster
---> 19     desc = arcpy.Describe(raster)
     20     inras = desc.CatalogPath
     21     print(inras)

C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py in Describe(value, data_type)
   1241        class with the same name. In this case, the data type is used to clarify
   1242        which dataset you want to describe."""
-> 1243     return gp.describe(value, data_type)
   1244 
   1245 def CreateObject(name, options=None):

C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py in describe(self, *args)
    366         from arcpy.arcobjects.arcobjectconversion import convertArcObjectToPythonObject
    367         return convertArcObjectToPythonObject(
--> 368                     self._gp.Describe(*gp_fixargs(args, True)))
    369     def createObject(self, *args):
    370         """GP function CreateObject"""

OSError: "fire_intensity_42_img" does not exist