3

I'm trying to print raster batch count number in PyScripter and ArcGIS 10.3 I'm using the next code:

import arcpy

carpeta = arcpy.env.workspace = r"C:\Base_de_datos\Cuencas_raster"

for raster in carpeta:
    with arcpy.da.SearchCursor(raster, ("COUNT")) as cursor:
        for row in cursor:
            count = row[0]
            print raster + "," + count

PyScripter give the next error:

Traceback (most recent call last): File "", line 16, in RuntimeError: cannot open 'C'

Any idea to how resolve this?

  • ListRasters first, iterate through them using rstr=arcpy.Raster(raster) and work with rstr. – FelixIP Nov 12 '15 at 1:33
6

Firstly you are looping through all the characters in carpeta not all the rasters;

i.e.

carpeta = arcpy.env.workspace = r"C:\Base_de_datos\Cuencas_raster"

for raster in carpeta:
    print raster

C
:
\
B
a

etc...

You need to use ListRasters (or arcpy.da.walk).

Secondly, you need to pass a list or tuple of field names. You are passing a single string when you pass ("COUNT") as a single element tuple requires a trailing comma.

Pass ["COUNT"] or ("COUNT",)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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