I have a code in the following form. The problem i have is that some shapefiles don't have the specific fields DISTANCE, DURATION, so i tried to write a code with ListFields so as not to exit the commands but to go to the next shapefile. Unfortunately when the script finds a shp with fields which don't have DISTANCE DURATION, it terminates the procedure. Any help would be welcome. This code is in a try except
This is the code:
import arcpy
import os
from arcpy import env
Routesworkspace = arcpy.GetParameterAsText(2)
env.workspace = Routesworkspace
cases = ['RCc10_3_S', 'RCc20_3_S', 'RCc30_3_S', 'RCc40_3_S', 'RCc50_3_S']
SUMDISTANCE = 0
C = 0
for fc in arcpy.ListFeatureClasses():
for case in cases:
if fc.startswith(case):
fields = ['DISTANCE', 'DURATION']
fieldList = arcpy.ListFields(fc)
if fields in fieldList:
with arcpy.da.SearchCursor(fc, fields, "FID = 0") as cursor:
a = fc
for row in cursor:
DISTANCE = row[0]
DURATION = row[1]
SUMDISTANCE = SUMDISTANCE + DISTANCE
C = C + 1
if C > 0:
AVGDISTANCE = SUMDISTANCE / C
outFile.write('' + str(int(round(AVGDISTANCE))) + '\n')
else:
outFile.write('' + str(0) + '\n')