I've a fairly extensive script which requires the user to manually define a variable which populates rest of script such as file paths, file names and outputs etc. Now I'm trying get the scrip to loop through a table of file prefixes (I've these available as polygons geodatabase as well). I thought best way to accomplish this would be using Search Cursors on a table where I've file prefix saved in each row, so if I were to set up a loop it would grab the row value, substitute it in place of the variable (coName) and then goes through rest of the steps in the script. So I've tried the following, but I'm having a difficult time trying to figure out: (1) how to tell SC to obtain a value and then insert it as the variable (2) in other variations it pulls the loops through the table and then ends up using the last row item in the table.

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

# Table with prefix file names such as ABCD_10001, ABDC_10002 etc.,
fTable = "C:/test_table.txt"
field = "FileName"

cursor = arcpy.da.SearchCursor(fTable,field)

# Iterate through each rows
row = cursor.next()
while row:
    print (row.getValue(field))

    coName = row
    wFolder = "C:/Imagery/"
    coGDB = os.path.join(str(coName) + ".gdb/")

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

    DEV = os.path.join(str(wFolder), str(coGDB), str(coName) + "_DEV")
    print (arcpy.Exists(DEV))

    outRas = Raster(DEV) * 2
    outRas.Save(str(coName) + "_x2")


I'm using ArcGIS 10.4. The last two lines are a psuedo map algebra equation.

  • Whatever the entire row looks like, it's probably not suitable for the name of a .gdb. Perhaps you meant to set coName to row.getValue(field)?
    – J Kelly
    Sep 15, 2016 at 0:09
  • The rows are "ABCD_10001, ABCD_10002,..." so no numerical values as the gdb name. I don't believe I tried row.getValue(field) yet. Also, I just noticed this but should cursor.next() be after the map algebra? So this way it knows that after it has retrieved the value, used it as a variable and performed spefic steps...It needs to move on to the next row.
    – user81784
    Sep 15, 2016 at 0:15
  • row.getValue() does not exist in DA cursors. Using the with/for looping construct saves you on using next() (which should be at the top or bottom)
    – Vince
    Sep 15, 2016 at 1:53

1 Answer 1


I'm not sure what you're doing with the last two lines, so for now I'm leaving them out of the script, but for the rest take a look at the following:

import arcpy,os

# Table with prefix file names such as ABCD_10001, ABDC_10002 etc.,
fTable = "C:/test_table.txt"
field = "FileName"

with arcpy.da.SearchCursor(fTable, field) as cursor:
    for row in cursor:
        print row[0] 

        coName = row[0] # coName is now holding the value of FileName for the current row of your table

        wFolder = r"C:\Imagery"
        coGDB = os.path.join("{}.gdb".format(coName))

        arcpy.env.workspace = os.path.join(wFolder, coGDB)
        print (arcpy.Exists(arcpy.env.workspace))

        DEV = os.path.join(wFolder, coGDB, "{}_DEV".format(coName))
        print arcpy.Exists(DEV)
        print DEV

Therow is the entire contents of a line in your table. row[0] is the first item of that line. If you want to get your filename prefix you need to use row[0].

The value output from the line print dev should be something like C:\Imagery\ABCD_10001.gdb\ABCD_10001_DEV.

As for the last two lines, I'm not sure what you're trying to do here.

outRas = DEV * 2
outRas.Save(str(coName) + "_x2")

Doing a print DEV * 2 will produce C:\Imagery\ABCD_10001.gdb\ABCD_10001_DEVC:\Imagery\ABCD_10001.gdb\ABCD_10001_DEV, and the line outRas.Save probably won't do anything as you're just telling a string variable to save, which will error out.

  • Apologies. The last two lines were a poor attempt to illustrate one of several map algebra steps in the script. I should have included from arcpy.sa import * and used appropriate syntax to define DEV raster such as outRas = Raster(DEV) * 2. I hope that explains what I'm trying to do there. I won't be able to implement this until tomorrow. I'll share my results then. Thanks! Update: I've made the changes to the code in the question.
    – user81784
    Sep 15, 2016 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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