1

I have a script that runs perfectly in the "Field Calculator." The codeblock looks like this:

def timeday(time, am, diur, pm, noc):
    if(time >=am and time < diur):
        return "crep_am"
    elif(time>= diur and time<pm):
        return "diurnal"
    elif(time>=pm and time<noc):
        return "crep_pm"
    else:
        return "nocturnal"

and the expression looks like this:

timeday(!LMT_TIME!, !B_C_AM1!, !B_DIUR1!, !B_C_PM1!, !BEG_NOC1!)

As I mentioned, I ran this on a single attribute table for a shapefile and it worked perfectly, but I've never used the python window before. I want to run this code on all of 100 shapefiles. I've been playing around with it, but I can't seem to get it right. Here's what I have so far:

import arcpy
arcpy.env.workspace = r"C:\Users\evan\Desktop\Evan_4-
2017\Deer_Data\LaSal_Deer\GPS_Data\GEO_WGS1984"
Shapefiles = arcpy.ListFeatureClasses("junktest.shp", "junktest2.shp")
codeblock= """def timeday(time, am, diur, pm, noc):
if(time >=am and time < diur):
    return "crep_am"
elif(time>= diur and time<pm):
    return "diurnal"
elif(time>=pm and time<noc):
    return "crep_pm"
else:
    return "nocturnal""""


for shp in Shapefiles:
    arcpy.CalculateField_management(shp,"TIME_DAY","timeday(!LMT_TIME!, !B_C_AM1!, !B_DIUR1!, !B_C_PM1!, !BEG_NOC1!)","PYTHON",  codeblock)

Alternatively, if there is a way to run the field calculator in ArcMap on all of the shapefiles as a batch, I'd be fine with that, but when I tried doing that I was not able to enter my codeblock, only the expression.

  • 2
    Can you replace your pictures with the actual code? – Paul May 10 '17 at 19:44
  • You can do this with Calculate Field tool and batch processing. There are parameters for both expression and code block in the Calculate Field tool. – reevesii May 10 '17 at 19:47
  • @reevesii, when I try and do batch processing I'm not able to enter a code block with more than one line into the "Code Block" column. I realize this is probably something simple, but I cannot figure it out. – J.W. Powell May 10 '17 at 20:01
  • put an @ in front of users names to notify them. Double click on that cell and it will bring up a new window. – reevesii May 10 '17 at 20:04
  • @reevesii a double-click is all that was needed. Thanks. – J.W. Powell May 10 '17 at 20:06
2

Your code should work, however it looks like the indentation is wrong on your code block.

import arcpy
arcpy.env.workspace = r"C:\Users\evan\Desktop\Evan_4-
2017\Deer_Data\LaSal_Deer\GPS_Data\GEO_WGS1984"
Shapefiles = arcpy.ListFeatureClasses("junktest.shp", "junktest2.shp")
codeblock= """def timeday(time, am, diur, pm, noc):
    if(time >=am and time < diur):
        return "crep_am"
    elif(time>= diur and time<pm):
        return "diurnal"
    elif(time>=pm and time<noc):
        return "crep_pm"
    else:
        return "nocturnal" """

for shp in Shapefiles:
    arcpy.CalculateField_management(shp,"TIME_DAY","timeday(!LMT_TIME!, !B_C_AM1!, !B_DIUR1!, !B_C_PM1!, !BEG_NOC1!)","PYTHON",  codeblock)

To get a usable python snippet after you've run something successfully in the Field Calculator, from the Geoprocessing menu select Results, then find the successful Calculate Field run (Field Calculator uses the geoprocessing tool behind the scenes), right-click on the result and select Copy Python Snippet. Then paste that into a text editor so you can add your other info like your shapefile loop.

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.