1

I'm trying to run this loop that pulls a detection time out of one feature class, sets it as the time variable, and then uses that time to select the entries in another feature class that are either before or after that time. There are multiple different detections, so I need it to loop through all of them in the gdb. It works for the first detection, but once it loops back to the next feature class is gives me an IOError saying that it can't find the Detection2 when it gets to the first search cursor. But when I go to that geodatabase, Detection2 is there. What's going on here?

import arcpy
import math
import os
from datetime import datetime

#################IDENTIFYING BEFORE AND AFTER TIMES FOR EACH MMMSI FOR EACH DETECTION TO GET MINS AND MAX ######################
arcpy.env.workspace = "L:\\gathr\\indonesia\\Sara\\Date20160102\\Detections_UTM.gdb"
Geodatabase = arcpy.ListFeatureClasses()
detection_datalist = []
print Geodatabase
for fc in Geodatabase:
    fc_name = str(fc)
    print fc_name
    detection_name = fc_name
    cursor = arcpy.SearchCursor(fc)
    for row in cursor:
        time1 = row.getValue("Date_Mscan")
        time2 = time1.strftime("%Y-%m-%d %H:%M:%S")
        print time2
        time = datetime.strptime(time2,"%Y-%m-%d %H:%M:%S")
        print time
    del cursor  
    arcpy.env.workspace = "L:\\gathr\\indonesia\\Sara\\Date20160102\\fc_processed.gdb"
    Geodatabase = arcpy.ListFeatureClasses()
    for fc in Geodatabase:
        fc_str = str(fc)
        suffix = detection_name
        if fc_str.endswith(suffix):
            layer = arcpy.MakeFeatureLayer_management(fc)
            processed_path = "L:\\gathr\\indonesia\\Sara\\Date20160102\\fc_processed.gdb"
            layer_processed = os.path.join(processed_path, fc + "_final")
            before = os.path.join(processed_path, fc + "_before")
            where_clause = '"ts_pos_utc_Converted" <= date' + "'%s'" %time
            arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", where_clause)
            arcpy.CopyFeatures_management(layer, before)

            listdates = []
            rows = arcpy.SearchCursor(before)
            row = rows.next()
            while row:
                value = row.ts_pos_utc_Converted
                print value
                listdates.append(value)
                row = rows.next()

            print listdates
            try:
                maxdate = max(listdates)
            except:
                maxdate = None 
            print maxdate

            after = os.path.join(processed_path, fc + "_after")
            where_clause = '"ts_pos_utc_Converted" > date' + "'%s'" %time
            arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", where_clause)
            arcpy.CopyFeatures_management(layer, after)

            listdates = []
            rows = arcpy.SearchCursor(after)
            row = rows.next()
            while row:
                value = row.ts_pos_utc_Converted
                print value
                listdates.append(value)
                row = rows.next()

            print listdates
            try:
                mindate = min(listdates)
            except:
                mindate = None
            print mindate

            layer = arcpy.MakeFeatureLayer_management(fc)                               
            arcpy.CopyFeatures_management(layer, layer_processed)
            with arcpy.da.UpdateCursor(layer_processed,"ts_pos_utc_Converted") as cursor:
                for row in cursor:
                    if row[0] not in [mindate, maxdate]:
                        cursor.deleteRow()

And here is the Error I'm getting.

Traceback (most recent call last): File "L:/gathr/indonesia/Sara/Scripts/1234.py", line 15, in cursor = arcpy.SearchCursor(fc) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy__init__.py", line 1179, in SearchCursor return gp.searchCursor(dataset, where_clause, spatial_reference, fields, sort_fields) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\geoprocessing_base.py", line 359, in searchCursor self._gp.SearchCursor(*gp_fixargs(args, True))) IOError: "Detection2" does not exist

2

Got it to work. Turned out that instead of putting 'fc' as the variable for the Search Cursor, I needed to include the full path name.

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.